用手指做一个UIView拖动

时间:2013-02-04 09:42:22

标签: ios uiview draggable uitapgesturerecognizer touchesmoved

我有mainViewController,但当我点击UIView中的MKAnnotationView时,我的内部MKMapKit会被激活,所以我需要UIView在屏幕的任何部分都可以拖动。

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

enter image description here

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

我尝试使用UITapGestureRecognizer,但它没有用,因为我的代码不够好,我无法将其拖动,因为它只是点击,而不是点击和移动。

我希望你能帮助我。

4 个答案:

答案 0 :(得分:11)

  1. 使用UIPanGestureRecognizer代替UITapGestureRecognizer
  2. 为您的观点设置userInteractionEnabled = YES
  3. 查看有关手势识别器的精彩教程:Touches。有很好的拖动视图的例子。

答案 1 :(得分:3)

答案 2 :(得分:2)

在@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;

答案 3 :(得分:1)

我用它在我的应用程序中缩放和拖动UIView。

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