Ios 如何通过平移手势限制可移动视图
我有一个Ios 如何通过平移手势限制可移动视图,ios,ios6,uiimageview,orientation,uipangesturerecognizer,Ios,Ios6,Uiimageview,Orientation,Uipangesturerecognizer,我有一个UIImageView,可以通过平移手势移动 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; [self.photoMask addGestureRecognizer:pan]; 我想限制屏幕上可以移动的区域。我不希望用户能够将视图向右拖动到屏幕的一侧,而是希望通过某种方式限制它。我该怎么做 另外,旋转时如何处
UIImageView
,可以通过平移手势移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.photoMask addGestureRecognizer:pan];
我想限制屏幕上可以移动的区域。我不希望用户能够将视图向右拖动到屏幕的一侧,而是希望通过某种方式限制它。我该怎么做
另外,旋转时如何处理
编辑--
这是我目前处理平底锅的方法。我需要做的是继续按中心点移动imageview,并限制其在靠近屏幕边缘时的移动,例如50。我不确定我在这里是否过于简单,但我认为可以通过使用if子句来实现这一点
-(void)handlePan:(UIPanGestureRecognizer*)gesture {
UIImageView *viewToDrag = gesture.view; // this is the view you want to move
CGPoint translation = [gesture translationInView:viewToDrag.superview]; // get the movement delta
CGRect movedFrame = CGRectOffset(viewToDrag.frame, translation.x, translation.y); // this is the new (moved) frame
// Now this is the critical part because I don't know if your "margin"
// is a CGRect or maybe some int values, the important thing here is
// to compare if the "movedFrame" values are in the allowed movement area
// Assuming that your margin is a CGRect you could do the following:
if (CGRectContainsRect(yourPermissibleMargin, movedFrame)) {
CGPoint newCenter = CGPointMake(CGRectGetMidX(movedFrame), CGRectGetMidY(movedFrame));
viewToDrag.center = newCenter; // Move your view
}
// -OR-
// If you have your margins as int values you could do the following:
if ( (movedFrame.origin.x + movedFrame.size.width) < 50) {
CGPoint newCenter = CGPointMake(CGRectGetMidX(movedFrame), CGRectGetMidY(movedFrame));
viewToDrag.center = newCenter; // Move your view
}
}
-(void)手持面板:(UIPangestureRecognitor*)手势{
UIImageView*viewToDrag=SISTER.view;//这是要移动的视图
CGPoint translation=[手势转换视图:viewToDrag.superview];//获取移动增量
CGRect movedFrame=CGRectOffset(viewToDrag.frame,translation.x,translation.y);//这是新的(移动的)帧
//现在这是关键部分,因为我不知道你的“利润”
//是一个CGRect或者一些int值,这里重要的是
//比较“movedFrame”值是否在允许的移动区域
//假设您的保证金是CGRect,您可以执行以下操作:
if(CGRectContainsRect(您的许可证argn,movedFrame)){
CGPoint newCenter=CGPointMake(CGRectGetMidX(movedFrame),CGRectGetMidY(movedFrame));
viewToDrag.center=newCenter;//移动视图
}
//-或-
//如果将边距设置为int值,则可以执行以下操作:
if((movedFrame.origin.x+movedFrame.size.width)<50){
CGPoint newCenter=CGPointMake(CGRectGetMidX(movedFrame),CGRectGetMidY(movedFrame));
viewToDrag.center=newCenter;//移动视图
}
}
您可能需要对其进行调整以满足您的特定需求
希望这有帮助 一个可能的解决方案是使用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;
}
}
检查该点是否在您想要的范围内,如果是,请将图像视图框的中心设置为该点。以下是Swift 4-
- (void)dragAction:(UIPanGestureRecognizer *)gesture{
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
if (CGRectContainsPoint(label.frame, [gesture locationInView:label] )) {
label.center = CGPointMake(label.center.x,
label.center.y);
[gesture setTranslation:CGPointZero inView:label];
}
else{
label.center = CGPointMake(label.center.x,
label.center.y + translation.y);
[gesture setTranslation:CGPointZero inView:label];
}
}
将视图的移动限制为superview
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer)
{
// Allows smooth movement of stickers.
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed
{
let point = gestureRecognizer.location(in: self.superview)
if let superview = self.superview
{
let restrictByPoint : CGFloat = 30.0
let superBounds = CGRect(x: superview.bounds.origin.x + restrictByPoint, y: superview.bounds.origin.y + restrictByPoint, width: superview.bounds.size.width - 2*restrictByPoint, height: superview.bounds.size.height - 2*restrictByPoint)
if (superBounds.contains(point))
{
let translation = gestureRecognizer.translation(in: self.superview)
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.superview)
}
}
}
}
如果您想对其进行更多控制,请将restrictByPoint值与移动视图的框架相匹配。谢谢,我已为我的问题添加了一个编辑。目前,我从中心点移动视图。这样,我移动的图像视图(中间有一个圆)看起来像是圆在移动。。。我正在尝试更新您的答案,以纳入此中心点,但不确定如何更新?我想继续提出中间观点(然后检查此中间点是否在屏幕的限制区域内。例如,整个屏幕周围有50个边距?如果您尝试快速拖动视图,则此if条件将中断。谢谢,我已对我的问题添加了一个编辑。目前,我从中心点移动视图。这样,我移动的图像视图(中间有一个圆)使它看起来像是在移动…我正在尝试更新你的答案以包含这个中心点,但不确定如何添加?我希望继续移动视图的中间点(然后检查该中点是否在屏幕的限制区域内。例如,整个屏幕周围有50个边距?使用MAX(minFloat,MIN(x,maxFloat))
- (void)dragAction:(UIPanGestureRecognizer *)gesture{
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
if (CGRectContainsPoint(label.frame, [gesture locationInView:label] )) {
label.center = CGPointMake(label.center.x,
label.center.y);
[gesture setTranslation:CGPointZero inView:label];
}
else{
label.center = CGPointMake(label.center.x,
label.center.y + translation.y);
[gesture setTranslation:CGPointZero inView:label];
}
}
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer)
{
// Allows smooth movement of stickers.
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed
{
let point = gestureRecognizer.location(in: self.superview)
if let superview = self.superview
{
let restrictByPoint : CGFloat = 30.0
let superBounds = CGRect(x: superview.bounds.origin.x + restrictByPoint, y: superview.bounds.origin.y + restrictByPoint, width: superview.bounds.size.width - 2*restrictByPoint, height: superview.bounds.size.height - 2*restrictByPoint)
if (superBounds.contains(point))
{
let translation = gestureRecognizer.translation(in: self.superview)
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.superview)
}
}
}
}