我有,mainViewController
但我有一个小的UIView
内部,当您在 中点击 a 时会激活MKAnnotationView
它MKMapKit
,所以我需要它UIView
可以在屏幕的任何部分拖动。
我的应用程序的示例屏幕截图:
圆圈是点的一个例子,我想我可以拖动“小”UIView 的任何点。
我尝试使用UITapGestureRecognizer
但它不起作用,因为我的代码不够好,而且我无法使其可拖动,因为它只是点击,而不是点击和移动。
我希望你能帮助我。
我有,mainViewController
但我有一个小的UIView
内部,当您在 中点击 a 时会激活MKAnnotationView
它MKMapKit
,所以我需要它UIView
可以在屏幕的任何部分拖动。
我的应用程序的示例屏幕截图:
圆圈是点的一个例子,我想我可以拖动“小”UIView 的任何点。
我尝试使用UITapGestureRecognizer
但它不起作用,因为我的代码不够好,而且我无法使其可拖动,因为它只是点击,而不是点击和移动。
我希望你能帮助我。
UIPanGestureRecognizer
代替UITapGestureRecognizer
userInteractionEnabled = YES
为您的视图用于创建可拖动和可调整大小的UIView
这个例子(带有源代码)对你非常有用。
并且还阅读了This Document和This Document This document is related toUIPanGestureRecognizer
在@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;
我用它来缩放和拖动我的应用程序中的 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;
}