#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //======================高亮============== _image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2"] highlightedImage:[UIImage imageNamed:@"3"]];//高亮狀態時切換成第二個圖片 _image.frame=CGRectMake(20, 30, 100, 100);//設置圖片大小 UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(130, 90, 80, 40)]; //設置按鈕的顯示名稱, [button setTitle:@"高亮切換" forState:UIControlStateNormal]; button.backgroundColor=[UIColor redColor];//設置背景顏色 [button addTarget:self action:@selector(gaoliang) forControlEvents:UIControlEventTouchUpInside];//給按鈕添加事件 [self.view addSubview:button]; [self.view addSubview:_image]; //=============給圖片添加手勢================ //實例化的時候直接添加圖片 _image1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]]; _image1.frame=CGRectMake(220, 30, 100, 100); [self.view addSubview:_image1]; //設置圖片支持交互 _image1.userInteractionEnabled=YES; //定義手勢 UITapGestureRecognizer *gestur=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shoushi)]; //將圖片添加到手勢 [_image1 addGestureRecognizer:gestur]; //==============利用Frame控制縮放================= _image2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4"]]; _image2.frame=CGRectMake(120, 150, 100, 100); [self.view addSubview:_image2]; UIButton *suoxiao=[[UIButton alloc]initWithFrame:CGRectMake(80, 260, 60, 40) ]; [suoxiao setTitle:@"縮小" forState:UIControlStateNormal]; [suoxiao addTarget:self action:@selector(suoxiao) forControlEvents:UIControlEventTouchUpInside]; suoxiao.backgroundColor=[UIColor redColor]; UIButton *fangda=[[UIButton alloc]initWithFrame:CGRectMake(180, 260, 60, 40)]; [fangda setTitle:@"放大" forState:UIControlStateNormal]; [fangda addTarget:self action:@selector(fangda) forControlEvents:UIControlEventTouchUpInside]; fangda.backgroundColor=[UIColor redColor]; [self.view addSubview:suoxiao]; [self.view addSubview:fangda]; } -(void)suoxiao{ _image2.transform=CGAffineTransformMakeScale(0.5, 0.5); } -(void)fangda{ _image2.transform=CGAffineTransformMakeScale(2, 5); } //手勢方法 -(void)shoushi{ _image2.transform=CGAffineTransformMakeScale(1, 1); NSLog(@"圖片手勢方法"); } //高亮方法 -(void)gaoliang{ if (_image.highlighted==YES) { _image.highlighted=NO; }else{ _image.highlighted=YES; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end