//定義手勢 UITapGestureRecognizer *gestur=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shoushi)]; //將圖片添加到手勢 [_image1 addGestureRecognizer:gestur]; //添加手勢---左劃 UISwipeGestureRecognizer *left=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)]; left.direction=UISwipeGestureRecognizerDirectionLeft;//設置手勢方向 [self.view addGestureRecognizer:left];//添加到屏幕上 //添加手勢---右劃 UISwipeGestureRecognizer *right=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)]; right.direction=UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:right]; //==============================高亮切換 - (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]; } //高亮方法--->默認顯示第一張圖片,點擊按鈕高亮切換後,顯示高亮圖片 -(void)gaoliang{ if (_image.highlighted==YES) { _image.highlighted=NO; }else{ _image.highlighted=YES; } }