我正在移动我的观点
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[bubbleView[rightCnt] addGestureRecognizer:panRecognizer];
[panRecognizer release];
现在,我希望通过长按拖动来做同样的事情。
任何的想法?
UILongPressGestureRecognizer
已经为你做了你想做的事。看看UIGestureRecognizerState
酒店。来自documentation:
长按手势是连续的。当指定时间段(minimumPressDuration)按下允许的手指数(numberOfTouchesRequired)并且触摸不超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan)。每当手指移动时,手势识别器转换到改变状态,并且当任何手指被抬起时,手势识别器结束(UIGestureRecognizerStateEnded)。
因此,基本上在调用UILongPressGestureRecognizer
selector之后,您将收听UIGestureRecognizerStateBegan,UIGestureRecognizerStateChanged,UIGestureRecognizerStateEnded。在UIGestureRecognizerStateChanged
期间继续更改您的视图框架。
- (void)moveRight:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan)
{
//if needed do some initial setup or init of views here
}
else if(gesture.state == UIGestureRecognizerStateChanged)
{
//move your views here.
[yourView setFrame:];
}
else if(gesture.state == UIGestureRecognizerStateEnded)
{
//else do cleanup
}
}
@implementation MyViewController {
CGPoint _priorPoint;
}
- (void)moveRight:(UILongPressGestureRecognizer *)sender {
UIView *view = sender.view;
CGPoint point = [sender locationInView:view.superview];
if (sender.state == UIGestureRecognizerStateChanged) {
CGPoint center = view.center;
center.x += point.x - _priorPoint.x;
center.y += point.y - _priorPoint.y;
view.center = center;
}
_priorPoint = point;
}
在Swift中,这可以使用下面的代码来实现
class DragView: UIView {
// Starting center position
var initialCenter: CGPoint?
override func didMoveToWindow() {
super.didMoveToWindow()
// Add longPress gesture recognizer
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(gesture:)))
addGestureRecognizer(longPress)
}
// Handle longPress action
func longPressAction(gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
guard let view = gesture.view else {
return
}
initialCenter = gesture.location(in: view.superview)
}
else if gesture.state == .changed {
guard let originalCenter = initialCenter else {
return
}
guard let view = gesture.view else {
return
}
let point = gesture.location(in: view.superview)
// Calculate new center position
var newCenter = view.center;
newCenter.x += point.x - originalCenter.x;
newCenter.y += point.y - originalCenter.y;
// Update view center
view.center = newCenter
}
else if gesture.state == .ended {
...
}
}
您不需要声明_priorPoint;
在我的情况下,我只希望视图水平移动,所以我只是改变x坐标。
这是我的解决方案:
if (longpressGestRec.state == UIGestureRecognizerStateChanged)
{
UIView *view = longpressGestRec.view;
// Location of the touch within the view.
CGPoint point = [longpressGestRec locationInView:view];
// Calculate new X position based on the amount the gesture
// has moved plus the size of the view we want to move.
CGFloat newXLoc = (item.frame.origin.x + point.x) - (item.frame.size.width / 2);
[item setFrame:CGRectMake(newXLoc,
item.frame.origin.y,
item.frame.size.width,
item.frame.size.height)];
}
感谢Hari Kunwar的Swift代码,但是没有正确定义longPressAction函数。
这是一个改进版本:
@objc func longPressAction(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
}
else if gesture.state == .changed {
guard let view = gesture.view else {
return
}
let location = gesture.location(in: self.view)
view.center = CGPoint(x:view.center.x + (location.x - view.center.x),
y:view.center.y + (location.y - view.center.y))
}
else if gesture.state == UIGestureRecognizerState.ended{
}
}