用手指使 UIView 可拖动
Posted
技术标签:
【中文标题】用手指使 UIView 可拖动【英文标题】:Make a UIView draggable with the finger 【发布时间】:2013-01-18 23:46:02 【问题描述】:我有mainViewController
,但我有一个小的UIView
,当您在MKMapKit
中点击MKAnnotationView
时会激活它,所以我需要UIView
可以在屏幕的任何部分拖动.
我的应用的示例截图:
圆圈是点的一个例子,我想我可以拖动“小”UIView的任何点。
我尝试使用UITapGestureRecognizer
,但它不起作用,因为我的代码不够好,而且我无法使其可拖动,因为它只是点击,而不是点击和移动。
我希望你能帮助我。
【问题讨论】:
【参考方案1】:-
使用
UIPanGestureRecognizer
而不是UITapGestureRecognizer
设置userInteractionEnabled = YES
供您查看
查看这个关于手势识别器的精彩教程:Touches。有一个很好的拖动视图的例子。
【讨论】:
感谢您的回答,我会研究我测试结果的那个项目,我认为它可以帮助我很多,谢谢。【参考方案2】:For create draggable and resizable UIView
This example (with source code) really useful for you.
同时阅读This Document和This Document这个文档与UIPanGestureRecognizer
相关
【讨论】:
【参考方案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;
【讨论】:
"Changed" 不会被点击手势调用,因为它没有意义。 没错,我会编辑帖子指向UIPanGestureRecognizer
【参考方案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;
【讨论】:
以上是关于用手指使 UIView 可拖动的主要内容,如果未能解决你的问题,请参考以下文章