1

皆さんこんにちは、

初めて「タッチメソッド」を使用しているので、特定の問題に直面しています。私のアプリでは、mainview に 2 つのサブビューがあります。あるビューから別のビューへの uibutton のドラッグ アンド ドロップを実装しようとしています。いろいろ試しているのですが、ドラッグアンドドロップができません。

私を助けてください。

前もって感謝します。

My Code :


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
        for (UIButton *iView in self.view.subviews) {
            if ([iView isMemberOfClass:[UIButton class]])
            {
                if (touchPoint.x > iView.frame.origin.x &&
                    touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
                    touchPoint.y > iView.frame.origin.y &&
                    touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
                {
                    self.btnalpha = iView;
                    touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
                                                   touchPoint.y - iView.frame.origin.y);

                    homePosition = CGPointMake(iView.frame.origin.x,
                                                    iView.frame.origin.y);

                    [self.viewblue bringSubviewToFront:self.btnalpha];
                }
            }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];

    CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
                                           touchPoint.y - touchOffset.y,
                                           self.btnalpha.frame.size.width,
                                           self.btnalpha.frame.size.height);
    self.btnalpha.frame = newDragObjectFrame;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];

    if (touchPoint.x > self.vieworange.frame.origin.x &&
        touchPoint.x < self.vieworange.frame.origin.x + self.vieworange.frame.size.width &&
        touchPoint.y > self.vieworange.frame.origin.y &&
        touchPoint.y < self.vieworange.frame.origin.y + self.vieworange.frame.size.height )
    self.btnalpha.frame = CGRectMake(self.homePosition.x, self.homePosition.y, self.btnalpha.frame.size.width, self.btnalpha.frame.size.height);
}

- (void) drag
{
    str_scramble =@"IH";

    for (int i = 0; i < 2; i++)
    {
        self.btnalpha = [[UIButton alloc] init];
        [self.btnalpha setFrame:CGRectMake(i * 50 + 10, 5, 42, 42)];
        char letter = [str_scramble characterAtIndex:i];
        self.btnalpha.backgroundColor = [UIColor grayColor];
        self.btnalpha.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        [self.btnalpha setTitle:[NSString stringWithFormat:@"%c", letter] forState:UIControlStateNormal];
        [self.btnalpha addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
        [self.btnalpha addTarget:self action:@selector(touchesMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [viewblue addSubview:self.btnalpha];
        NSLog(@"string : --> %@", self.btnalpha.titleLabel.text);
    }
}
4

3 に答える 3

0

UIButton をドラッグするには、touchDragEnter、touchDragExit、touchDragInside、または touchDragOutside などの touchDrag メソッドを使用する必要があります。

これらのすべてのイベントは UIControlEvents です

于 2013-03-04T09:20:46.100 に答える
0

イベント処理ガイドのAppleドキュメントによると、
最初にボタンをタッチすると、最初のresponder.SOになり、ViewControllerのtouchesBegan:withEventは呼び出されません。

UIbutton の userInteraction を無効にすると、viewcontroller が最初のレスポンダーになり、タッチ イベントの受信を開始します。
button.userInteractionEnabled=NO;

ViewController またはそれらのビューのいずれかにGesturesを追加しても、タッチがボタンにあるときに応答しません。

于 2013-12-18T11:33:49.423 に答える