限制添加到 UIPanGestureRecognizer 的视图在其超级视图中的移动

发布于 2024-10-01 16:48:34 字数 1752 浏览 6 评论 0原文

我想知道是否有一种方法可以在使用 GestureRecognizer 时将 UIView 的移动限制在其超级视图中。

例如。我有一个 UIImageview ,我向其添加带有操作 panPiece 的 UIPanGestureRecognizer 。我通过以下方式调整它的中心,以避免它移动出它的超级视图。但它不能正常工作。

-(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]];
    }
}

感谢代码帮助。

I want to know if there is a way to bound the movement of my UIView within its superview while using GestureRecognizer.

eg. I have a UIImageview To whom I add UIPanGestureRecognizer with action panPiece. I adjust its center in the following way to avoid its movement out of its superview. But it doesnt work properly.

-(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]];
    }
}

Code help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2024-10-08 16:48:34

您只需要使用超级视图的属性clipsToBounds(将其设置为YES)即可。之前也有同样的问题。之后就可以完美工作了。

You just need to use the property clipsToBounds (set it to YES) to the superview. Had the same issue earlier. Works perfectly after that.

弥枳 2024-10-08 16:48:34

或许是因为

标准 UIImageView,尽管是 UIView 子类,但不会对添加到其中的手势识别器做出反应

使用uigesturerecognizers

Perhaps it is because

standard UIImageViews, despite being UIView subclasses, do not react to gesture recognizer added to them

working with uigesturerecognizers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文