8

我有,mainViewController但我有一个小的UIView内部,当您在 中点击 a 时会激活MKAnnotationViewMKMapKit,所以我需要它UIView可以在屏幕的任何部分拖动。

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

在此处输入图像描述

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

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

我希望你能帮助我。

4

4 回答 4

12
  1. 使用UIPanGestureRecognizer代替UITapGestureRecognizer
  2. 设置userInteractionEnabled = YES为您的视图
  3. 查看这个关于手势识别器的精彩教程:Touches。有一个很好的拖动视图的例子。
于 2013-02-04T09:46:08.867 回答
3

用于创建可拖动和可调整大小的UIView这个例子(带有源代码)对你非常有用。

并且还阅读了This DocumentThis Document This document is related toUIPanGestureRecognizer

于 2013-02-04T09:54:39.933 回答
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;
于 2013-02-04T09:46:38.010 回答
1

我用它来缩放和拖动我的应用程序中的 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;
}
于 2013-11-23T14:05:57.347 回答