做一个UIView拖动用手指(Make a UIView draggable with the fi

2019-08-16 18:05发布

我有mainViewController ,但我有一个小UIView ,当你点击一个内被激活MKAnnotationViewMKMapKit ,所以我需要UIView为可拖动在屏幕的任何部分。

我的应用程序的示例屏幕截图:

圆是点的一个例子,我想我可以拖动为“小”的UIView的任一点。

我试着用UITapGestureRecognizer但我的代码是不是不够好它没有工作,我不能使它可拖动,因为它是唯一的水龙头,而不是挖掘和移动。

我希望你能帮助我。

Answer 1:

  1. 使用UIPanGestureRecognizer代替UITapGestureRecognizer
  2. 设置userInteractionEnabled = YES你的看法
  3. 查看有关手势识别这个漂亮的教程: 倒是 。 有拖动意见的很好的例子。


Answer 2:

对于创建可拖动和可调整大小UIView这个例子( 源代码 )为您真的很有用。

而且还阅读文档和文档本文档相关UIPanGestureRecognizer



Answer 3:

通过@borrrden一个称道后编辑

UIPanGestureRecognizer是合适的。 在处理函数中检查它的state变量。

typedef enum {
   UIGestureRecognizerStatePossible,

   UIGestureRecognizerStateBegan,     // this will be the value on touch
   UIGestureRecognizerStateChanged,   // ~ on drag
   UIGestureRecognizerStateEnded,     // ~ on end of touch event
   UIGestureRecognizerStateCancelled,

   UIGestureRecognizerStateFailed,

   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;


Answer 4:

我用这个缩放和我的应用程序拖拽的UIView。

1)首先,请确保以下添加到您的实现和连接网点的意见在你的故事板

#import <QuartzCore/QuartzCore.h>

@interface yourclass () {
    BOOL isDragging;
}
@property (weak, nonatomic) IBOutlet UIImageView *outletMainView; // View that contains the view we want to drag
@property (weak, nonatomic) IBOutlet UIImageView *outletRedDot; // The view we want to drag in the main view

当触摸开始我缩放视图,当用户触摸特定视图了一下,这里是红点

// ANIMATE RED DOT WHEN START DRAGING IT
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.outletMainView];

    CGRect redDotRect = [self.outletRedDot frame];
    if (CGRectContainsPoint(redDotRect, touchLocation)) {
        isDragging = YES;
        NSLog(@"Red Dot tapped!");
        [UIView animateWithDuration:0.2
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             self.outletRedDot.transform = CGAffineTransformMakeScale(1.75, 1.75);
                         }
                         completion:^(BOOL finished) {
                         }];
    } else {
        return;
    }
}

2)然后,我设置视图以跟随手指点的点

// ANIMATE AND MOVE RED DOT WHEN WE DRAG IT
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.outletMainView];

    if (isDragging) {
        [UIView animateWithDuration:0.0f
                              delay:0.0f
                            options:(UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut)
                         animations:^{
                             self.outletRedDot.center = touchLocation;
                             NSLog(@"X: %0.2f Y: %0.2f",touchLocation.x-redDotStartCenter.x, redDotStartCenter.y-touchLocation.y);
                         }
                         completion:NULL];
    }
}

3)最后,当拖动结束视图被复位到其原来的规模

// RESET RED DOT WHEN WE STOP DRAGGING
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.outletMainView];

    CGRect redDotRect = [self.outletRedDot frame];
    if (CGRectContainsPoint(redDotRect, touchLocation)) {
        [UIView animateWithDuration:0.1
                              delay:0.0
                            options:0
                         animations:^{
                             self.outletRedDot.transform = CGAffineTransformMakeScale(1.0, 1.0);
                         }
                         completion:^(BOOL finished) {
                         }];
    }
    isDragging = NO;
}


文章来源: Make a UIView draggable with the finger