gpt4 book ai didi

ios - 如何通过平移手势限制可移动 View

转载 作者:可可西里 更新时间:2023-11-01 03:40:01 25 4
gpt4 key购买 nike

我有一个 UIImageView,它可以通过平移手势移动。

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.photoMask addGestureRecognizer:pan];

我想限制可以在屏幕上移动的区域。我不想让用户能够将 View 直接拖到屏幕的一侧,而是想通过某种边距来限制它。我该怎么做?

此外,旋转时如何处理?

编辑---

#pragma mark - Gesture Recognizer
-(void)handlePan:(UIPanGestureRecognizer *)gesture {
NSLog(@"Pan Gesture");
gesture.view.center = [gesture locationInView:self.view];
}

这是我目前处理平底锅的方法。我需要做的是继续将 imageview 移动中心点,并在靠近屏幕边缘时限制其移动,例如 50。

最佳答案

一个可能的解决方案是在您的 handlePan 方法中,检查点在屏幕上的位置,并且只有在您希望将其限制在范围内时才提交更改。

例如

-(void) handlePan:(UIGestureRecognizer*)panGes{

CGPoint point = [panGes locationInView:self.view];

//Only allow movement up to within 100 pixels of the right bound of the screen
if (point.x < [UIScreen mainScreen].bounds.size.width - 100) {

CGRect newframe = CGRectMake(point.x, point.y, theImageView.frame.size.width, theImageView.frame.size.height);

theImageView.frame = newframe;

}

}

我相信这也能正确处理任何屏幕旋转

编辑

要将您的 ImageView 移动到其框架的中心,handlePan 方法可能看起来像这样。

-(void)handlePan:(UIPanGestureRecognizer *)gesture {

CGPoint point = [gesture locationInView:self.view];

//Only allow movement up to within 50 pixels of the bounds of the screen
//Ex. (IPhone 5)
CGRect boundsRect = CGRectMake(50, 50, 220, 448);

if (CGRectContainsPoint(boundsRect, point)) {
imgView.center = point;
}
}

检查该点是否在您想要的范围内,如果是,则将 ImageView 框架的中心设置为该点。

关于ios - 如何通过平移手势限制可移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17821617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com