使用手指使UIView可拖动

8
我有一个mainViewController,但是在里面有一个小的UIView。当你点击MKMapKit中的MKAnnotationView时,它就会激活,所以我需要这个UIView可以被拖动到屏幕的任何位置。
以下是我的应用程序的示例截图: enter image description here 圆圈是一个点的示例,我认为我可以拖动"小"的UIView的任何一个点。
我尝试使用UITapGestureRecognizer,但它没有起作用,因为我的代码不好,并且我不能使它可拖动,因为它只是点击,而不是点击和移动。
希望你能帮助我。
4个回答

12
  1. 请使用UIPanGestureRecognizer代替UITapGestureRecognizer
  2. userInteractionEnabled设置为YES以启用视图的用户交互功能
  3. 请查看这个很好的手势识别器教程:Touches。其中有一个漂亮的拖动视图的示例。

谢谢你的回答,我会研究那个项目。我测试了结果,我觉得它对我很有帮助,谢谢。 - Alejandro L.

3

2

如果涉及到UI控件的拖拽操作,UIPanGestureRecognizer 是比较适合的。在你的处理函数中,需要检查它的 state 变量。

Note: 本段文字已根据 @borrrden 的评论进行修改。

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;

由于没有意义,“Changed”不会在轻拍手势上被调用。 - borrrden
没问题,我会编辑帖子并指向UIPanGestureRecognizer - Alexander

1
I use this for scaling and dragging an UIView in my app.
1) 首先确保将以下内容添加到您的实现中,并将输出连接到您在Storyboard中的视图。
#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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接