GestureRecognizer を使用しているときに、スーパービュー内で UIView の動きを制限する方法があるかどうかを知りたいです。
例えば。アクションpanPieceでUIPanGestureRecognizerを追加するUIImageviewがあります。スーパービューの外に移動しないように、次の方法で中心を調整します。しかし、それは正しく動作しません。
-(CGPoint) centerWithBounds:(CGPoint)center andViewFrame:(CGRect)viewFrame andBoundingFrame:(CGRect)boundingFrame{
CGFloat lowerXBound = boundingFrame.origin.x + ( viewFrame.size.width/2 );
CGFloat higherXBound = boundingFrame.origin.x + boundingFrame.size.width - ( viewFrame.size.width/2 );
CGFloat lowerYBound = boundingFrame.origin.y + ( viewFrame.size.height/2 );
CGFloat higherYBound = boundingFrame.origin.y + boundingFrame.size.height - ( viewFrame.size.height/2 );
if ( center.x < lowerXBound) {
center.x = lowerXBound;
}
if ( center.x > higherXBound ){
center.x = higherXBound;
}
if ( center.y < lowerYBound) {
center.y = lowerYBound;
}
if ( center.y > higherYBound ){
center.y = higherYBound;
}
return center;
}
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint translatedCenter = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
CGPoint center = [self centerWithBounds:translatedCenter andViewFrame:[piece frame] andBoundingFrame:[[piece superview] frame]];
[piece setCenter:center];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
コードのヘルプは大歓迎です。