0

この色のボタンを特定のビューにドラッグすると、色がこのボタンの色に変わり、色のボタンが元の場所に再配置される必要があります。タッチの開始やタッチの終了など、さまざまな方法を試しましたが、問題が解決しないようです。さまざまな uicontrolstate で起動するカスタマイズされたメソッドも試しましたが、うまくいきませんでした。これで私を助けてください。

4

3 に答える 3

3

touchesBegan、touchesMoved、touchesEndedを使用すると問題を解決できると思います。

touchesBegan は、orignPosition をマークすることです。

CGPoint orignPosition;
CGColor buttonColor;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UIView *touchView = [touch view];
        if ([touchView isKindOfClass:[UIButton class]]) 
        {
            orignPosition = (UIButton *)touchView.center;
            buttonColor = (UIButton *)touchView.backgroundColor;
        }
}

touchesMoved は、ボタンを指で動かせるようにすることです

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UIView *touchView = [touch view];
    CGPoint movedPoint = [touch locationInView:yourMainView];
        CGPoint deltaVector = CGPointMake(movedPoint.x - lastTouchPoint.x, movedPoint.y - lastTouchPoint.y);
    lastTouchPoint = movedPoint;

    if ([touchView isKindOfClass:[UIButton class]])
        {
        touchView.center = CGPointMake(touchView.center.x + deltaVector.x, touchView.center.y + deltaVector.y);
        }
}

touchesEnded は、特別なビューの色を変更するかどうかを判断することです

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *touch = [touches anyObject];
    UIView *touchView = [touch view];
        CGPoint movedPoint = [touch locationInView:scrollView];
        if ([touchView isKindOfClass:[UIButton class]]) 
        {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.3];
            (UIButton *)touchView.center = orignPosition;
            [UIView commitAnimations];
        }
        if(CGRectContainsPoint(specialView.frame, [touch locationInView:yourMainView]))
        {
            [yourMainView setBackgroundColor:buttonColor];
        }
}
于 2012-07-25T13:47:46.937 に答える
1

ボタンを使用する必要はないと思います。UIView を作成し、背景色を設定し、パン ジェスチャを追加することができます。.h ファイルで、いくつかの変数を作成します。

UIView *green;
CGRect initialPosition;

initメソッドの.mファイルに次のようなものを追加します

initialPosition = CGRectMake(0, 0, 44, 44);
green = [[UIView alloc]initWithFrame:initialPosition];
green.backgroundColor = [UIColor greenColor];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(habdlePan:)]
[green addGestureRecognizer:green];
[panGesture release];
[self.view addSubview:green];
[green release];

HandleTap は次のようにする必要があります。

- (void) habdlePan:(UIPanGestureRecognizer *)panGesture {
    //Move your button here
    if(gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        //All fingers are lifted.
        //Return btn to init position
        [UIView beginAnimations:nil context:nil];
        green.frame = initialPosition;
        [UIView commitAnimations];
    }
}
于 2012-07-25T13:15:12.533 に答える
0

UIImageViewの代わりに使用することをお勧めしますUIButton。設定し[UIImageView userInractionEnabled:YES]ます。それが良いだろう。とメソッドimageViewを使用して簡単にドラッグできます。touchBegin:touchMove:

于 2012-07-25T13:43:56.567 に答える