2

このビュー内のボタンでいくつかの 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];
}

.

    - (void)dragBegan:(UIControl *)c withEvent:ev {

    UITouch *touch = [[ev allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

}

- (void)dragMoving:(UIControl *)c withEvent:ev {
    UITouch *touch = [[ev allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
 //This is moving view to touchPoint
SimpleView.center = touchPoint;


}

- (void)dragEnded:(UIControl *)c withEvent:ev {

}

そのボタンをロングプレスした場合、どうすれば移動できますか?

4

2 に答える 2

7

このコードを使用してみてください。私が開発したカードゲームでこれを使用しました。長押しジェスチャーを使用してカードを移動します。私が助けてくれることを願っています。

 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
 }
于 2013-10-11T03:21:56.147 に答える