Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何通过长按按钮然后拖动来移动UIView?_Ios_Objective C_Uiview_Long Press - Fatal编程技术网

Ios 如何通过长按按钮然后拖动来移动UIView?

Ios 如何通过长按按钮然后拖动来移动UIView?,ios,objective-c,uiview,long-press,Ios,Objective C,Uiview,Long Press,我想在此视图中按按钮移动一些UIView。 我可以这样做: - (void)viewDidLoad { [button addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown]; [button addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents

我想在此视图中按按钮移动一些UIView。 我可以这样做:

 - (void)viewDidLoad
    {
[button addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
        [button addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside];
        [button addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}

如果我长按那个按钮,我怎么才能移动它呢?

我会使用@Coder404提供的信息来检测播放器是否长按。然后,添加一个
@property BOOL performedLongTouch
,并在传递到
ui长按手势识别器的
选择器中将其设置为
YES

然后,在
dragBegan
dragMoving
函数中,添加对
performedLongTouch
的检查,并在
dragEnded
函数中,将其值设置为
NO


我知道这看起来很简单,但这就是你想要的吗?

尝试使用此代码。我在自己开发的一款纸牌游戏中使用过这个。使用长按手势移动卡片。希望我能帮忙

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)];
 [longPress setDelegate:self];
 [YOUR_VIEW addGestureRecognizer:longPress];

- (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender {

UIView *view = sender.view;

CGPoint point = [sender locationInView:view.superview];

if (sender.state == UIGestureRecognizerStateBegan){ 

  // GESTURE STATE BEGAN

}
else if (sender.state == UIGestureRecognizerStateChanged){

 //GESTURE STATE CHANGED/ MOVED

CGPoint center = view.center;
center.x += point.x - _priorPoint.x;
center.y += point.y - _priorPoint.y;
view.center = center;

// This is how i drag my views
}

else if (sender.state == UIGestureRecognizerStateEnded){

  //GESTURE ENDED
 }

你的意思是?是的,但我如何在这种情况下重新组织触摸事件?等等,你正在请求拖动和拖动方法中的
touch
的x和y值?如果是这样,请看。我知道,但如何在UILongPress手势识别器中使用它?需要长按然后再次触摸才能移动,但我需要通过长按并移动来完成!你不明白!我需要长按按钮,并拖动我的_视图,在相同的触摸!在UIButton上添加手势识别器,然后在UIgestureRecognitzerStateChanged中操纵MY_视图的点
 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)];
 [longPress setDelegate:self];
 [YOUR_VIEW addGestureRecognizer:longPress];

- (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender {

UIView *view = sender.view;

CGPoint point = [sender locationInView:view.superview];

if (sender.state == UIGestureRecognizerStateBegan){ 

  // GESTURE STATE BEGAN

}
else if (sender.state == UIGestureRecognizerStateChanged){

 //GESTURE STATE CHANGED/ MOVED

CGPoint center = view.center;
center.x += point.x - _priorPoint.x;
center.y += point.y - _priorPoint.y;
view.center = center;

// This is how i drag my views
}

else if (sender.state == UIGestureRecognizerStateEnded){

  //GESTURE ENDED
 }