2

隣り合わせに 2 つの UIButton があり、指を置くと両方とも白から黒に変わるとします。

それは明らかに正常に動作しますが、ボタン 1 に指を置くと黒くなり、指を離さずにボタン 2 の上に指を移動すると、ボタン 2 は黒くなりません。ボタン 1 はまだ黒です。

では、あるボタンを「スワイプ」してから別のボタンを「スワイプ」して、強調表示されたボタンを最初のボタンから2番目のボタンに変更するにはどうすればよいですか。

4

2 に答える 2

1

ビューコントローラーで touchesMoved イベントをオーバーライドします。これにより、タッチが発生したときに通知されます。問題は、ボタンがタッチ イベントを飲み込まないように、userInteractionEnabled を NO に設定する必要があることです。カスタム スライダーや実際のボタン イベントを必要としないものを実行しようとしている場合は、これで問題ありません。

次に、 touchesMoved でボタンをループして、どのボタンが押されているかを確認できます。ドラッグ入力時にボタンを強調表示するの優れた回答から盗まれたコードを次に示します

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *t in touches) {
    CGPoint touchPoint = [t locationInView:self.view];

    CGPoint testPoint = [self.view convertPoint:touchPoint toView:aButton];
    if ([aButton pointInside:testPoint withEvent:event]) {
        //Do something
    }
    //rest of code
于 2014-06-09T19:22:23.840 に答える
-1
[self.button1 addTarget:self
                 action:@selector(dragEnter:)
      forControlEvents:UIControlEventTouchDragEnter];
[self.button2 addTarget:self
                 action:@selector(dragEnter:)
       forControlEvents:UIControlEventTouchDragEnter];

[self.button1 addTarget:self
                 action:@selector(dragExit:)
       forControlEvents:UIControlEventTouchDragExit];
[self.button2 addTarget:self
                 action:@selector(dragExit:)
       forControlEvents:UIControlEventTouchDragExit];

- (void)dragEnter:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.highlighted = YES;
}

- (void)dragExit:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.highlighted = NO;
}
于 2013-04-13T22:57:34.023 に答える