在
touchesBegan
確保觸摸是圖像的視圖內(I假設_img
是UIImageView
類型):
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch = [[event allTouches] anyObject];
CGPosition touchPosition = [mytouch locationInView:self.view];
if (CGRectContainsPoint(_img.frame, touchPosition) && [touches count] == 1) {
_imageTouched = YES; // Declare _imageTouched in your class for this to work
}
}
CGRectContainsPoint
測試如果給定CGPoint
被withing給定CGRect
(這是圖像的幀中這種情況下)
然後在touchesMoved
:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch = [[event allTouches] anyObject];
if (_imageTouched) {
_img.center = [mytouch locationInView:self.view];
}
}
並且不要忘記在touchesEnd方法中設置_imageTouched
到NO
。
觸摸開始行動,你應該檢查是圖像幀包括用戶觸摸點。 –