我有一個小的imageView,使用UIPanGestureRecognizer
在屏幕上移動,但是當它從超級視圖的邊緣出來時沒有得到更多的觸摸,因此不能再回頭。我怎樣才能防止圖像從超級視圖的邊緣出來? 這是代碼:使用UIPanGestureRecognizer
- (void)pan:(UIPanGestureRecognizer *)gestureRecognizer
{
if (inLongPress) {
UIView *hitView = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if (!(CGRectContainsRect(self.imageView.frame, hitView.frame))) {
[UIView beginAnimations:@"" context:NULL];
[hitView setCenter:CGPointMake(_prevCenter.x, _prevCenter.y)];
[UIView commitAnimations];
}
_prevCenter=hitView.center;
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[hitView superview]];
[hitView setCenter:CGPointMake([hitView center].x + translation.x, [hitView center].y + translation.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[hitView superview]];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded){
inLongPress=NO;
hitView.alpha=1.0;
}
}
}
非常感謝你,它的工作原理! –