注意:1.imgeview默認不能響應觸摸事件,2.視圖有三個子視圖,如何區分多個視圖
用storyBoard托人2個view和一個imageView,IBOutlet連線
案例圖片:
1 [self.imageView setUserInteractionEnabled:YES];
1 [touch view] == self.imageView
1 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3 //1.獲取用戶點擊 4 UITouch *touch = [touches anyObject]; 5 CGPoint location = [touch locationInView:self.view]; 6 CGPoint PReLocation = [touch previousLocationInView:self.view]; 7 CGPoint dertPoint = CGPointMake(location.x - preLocation.x, location.y - preLocation.y); 8 //2.判斷點擊了那個視圖 9 if ([touch view] == self.imageView) {10 NSLog(@"點擊了圖像");11 12 [self.imageView setCenter:CGPointMake(self.imageView.center.x + dertPoint.x, self.imageView.center.y + dertPoint.y)];13 }else if ([touch view] == self.redView){14 NSLog(@"點擊了 hongse");15 [self.redView setCenter:CGPointMake(self.redView.center.x + dertPoint.x, self.redView.center.y + dertPoint.y)];16 17 }else if ([touch view] == self.greenView){18 //提示最好不要直接用else處理,因為隨時有可能添加新的控件19 NSLog(@"點擊 green");20 [self.greenView setCenter:CGPointMake(self.greenView.center.x + dertPoint.x, self.greenView.center.y + dertPoint.y)];21 }22 23 }
新聞熱點
疑難解答