我正在通过
移动我的观点UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[bubbleView[rightCnt] addGestureRecognizer:panRecognizer];
[panRecognizer release];
现在,我希望通过长按拖动来做同样的事情。
有什么想法吗?
答案 0 :(得分:83)
UILongPressGestureRecognizer
已经为您做了您想做的事。看看UIGestureRecognizerState
属性。来自documentation:
长按手势是连续的。手势开始了 (UIGestureRecognizerStateBegan)当允许手指的数量 (numberOfTouchesRequired)已按下指定的时间段 (minimumPressDuration)和触摸不会超越 允许的移动范围(allowableMovement)。手势 只要手指移动,识别器就会转换到更改状态, 当任何一个手指结束时(UIGestureRecognizerStateEnded) 被解除了。
因此,基本上在调用UILongPressGestureRecognizer
选择器后,您将收听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
}
}
答案 1 :(得分:36)
@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;
}
答案 2 :(得分:3)
在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 {
...
}
}
答案 3 :(得分:2)
您无需声明_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)];
}
答案 4 :(得分:1)
感谢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{
}
}