Ios 如何通过长按按钮然后拖动来移动UIView?
我想在此视图中按按钮移动一些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
- (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
}