9

から2 つの IBActionをドラッグしますUIButton。1 つは touchDown イベントで、もう 1 つはドラッグ インサイドです。

- (IBAction)clickButton:(UIButton *)sender {
    NSLog(@"Click Button");
}

- (IBAction)dragInsideButton:(UIButton *)sender {
    NSLog(@"Drag Button");
}

しかし、内側にドラッグすると、 touchDown アクションも発生します。

dragInside 時に touchDown イベントを無効にする方法。

ありがとう!

4

5 に答える 5

4

ドラッグイベントを使用してこのような問題を解決しました

.xibファイルまたはプログラムでボタンにイベントを追加します。

プログラム的には:

[mybut addTarget:self action:@selector(dragBegan:withEvent: )
  forControlEvents: UIControlEventTouchDown];
    [mybut addTarget:self action:@selector(dragMoving:withEvent: )
  forControlEvents: UIControlEventTouchDragInside];
    [mybut addTarget:self action:@selector(dragEnded:withEvent: )
  forControlEvents: UIControlEventTouchUpInside |
     UIControlEventTouchUpOutside];

次に、イベントの定義は次のとおりです。

- (void) dragBegan: (UIButton *) c withEvent:ev
{
    NSLog(@"dragBegan......");
    count=NO;//bool Value to decide the Down Event
    c.tag=0;
  [self performSelector:@selector(DownSelected:) withObject:mybut afterDelay:0.1];
 //user must begin dragging in 0.1 second else touchDownEvent happens

}

- (void) dragMoving: (UIButton *) c withEvent:ev
{
    NSLog(@"dragMoving..............");
    c.tag++;
}

- (void) dragEnded: (UIButton *) c withEvent:ev
{
    NSLog(@"dragEnded..............");
    if (c.tag>0 && !count)
    {

        NSLog(@"make drag events");

    }

}



-(void)DownSelected:(UIButton *)c
{
    if (c.tag==0) {
        NSLog(@"DownEvent");
        count=YES;//count made Yes To interrupt drag event
    }

}
于 2013-03-23T19:02:46.873 に答える
3

このメソッドはテストされており、あなたがやろうとしていると私が思うことをするはずです。タイマーの遅延を変更して、必要な効果を得ることができます。これを機能させるには、3 つのアクションをボタンに接続する必要がありました。3 つ目のアクションは、システムを開始状態にリセットする touchUp です。

@interface LastViewController ()
@property (nonatomic) BOOL touchedDown;
@property (strong,nonatomic) NSTimer *downTimer;
@end

@implementation LastViewController 

- (void)viewDidLoad {
    [super viewDidLoad];
    self.touchedDown = NO;
}

-(IBAction)clickDown:(id)sender {
    self.downTimer = [NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(buttonAction:) userInfo:nil repeats:NO];
}

-(IBAction)dragInside:(id)sender {
    [self.downTimer invalidate];
    [self buttonAction:self];
}


-(void) buttonAction:(id) sender {
    if ([sender isKindOfClass:[NSTimer class]]) {
        self.touchedDown = YES;
        NSLog(@"click down");
    }else{
        if (! self.touchedDown) {
             NSLog(@"Drag");
        }
    }
}

-(IBAction)touchUpAction:(id)sender {
    self.touchedDown = NO;
}
于 2013-03-23T17:28:26.517 に答える
1

この方法を試してください:

isClickedBOOL型のプロパティです。に設定しYESます。

- (IBAction)clickButton:(UIButton *)sender { //touch down
    if(isClick==YES){
       NSLog(@"Click Button");
       //do all your stuffs here
    }
    isClick=YES;
}

- (IBAction)dragInsideButton:(UIButton *)sender {
    NSLog(@"Drag Button");
    isClick=NO;
}

これに加えて、実装することもできますremoveTarget:Action:

どちらのメソッドが最初に呼び出されたとしても、isClick=NO を設定すると、ボタン アクションで clickButton が最初に呼び出されると予想されます。

于 2013-03-23T15:51:45.667 に答える
0

うまくいくかどうかわかりませんが、試してみてください

   - (IBAction)dragInsideButton:(UIButton *)sender {
    [sender removeTarget:self forSelector:@selector(clickButton:) forControlEvent:UIControlEventTouchDown];
}
于 2013-03-23T15:47:48.060 に答える
0

UIButton の 2 つのアクション メソッドから取得しました。次のトラックと前方へのシーク:

必要に応じて変更してください。

個人的には、ビューコントローラーまたはボタンサブクラス内の整数でボタンの状態を追跡するだけです。ボタンの動作を追跡すると、各アクションの動作を制御できます。.h ファイルに次のようなものを入れます。

enum {
    MyButtonScanning,
    MyButtonStalling,
    MyButtonIdle
};



@interface YourClass : UIViewController {
    NSInteger buttonModeAt;
}
@property (nonatomic) NSInteger buttonModeAt;
-(IBAction)buttonPushedDown:(id)sender;
-(void)tryScanForward:(id)sender;
-(IBAction)buttonReleasedOutside:(id)sender;
-(IBAction)buttonReleasedInside:(id)sender;
@end

そして、あなたの .m ファイルに次のようなものを入れます:

@implementation YourClass
///in your .m file
@synthesize buttonModeAt;


///link this to your button's touch down
-(IBAction)buttonPushedDown:(id)sender {
    buttonModeAt = MyButtonStalling;
    [self performSelector:@selector(tryScanForward:) withObject:nil afterDelay:1.0];
}

-(void)tryScanForward:(id)sender {
    if (buttonModeAt == MyButtonStalling) {
        ///the button was not released so let's start scanning
        buttonModeAt = MyButtonScanning;
        
        ////your actual scanning code or a call to it can go here
        [self startScanForward];
    }
}

////you will link this to the button's touch up outside
-(IBAction)buttonReleasedOutside:(id)sender {
    if (buttonModeAt == MyButtonScanning) {
        ///they released the button and stopped scanning forward
        [self stopScanForward];
    } else if (buttonModeAt == MyButtonStalling) {
        ///they released the button before the delay period finished
        ///but it was outside, so we do nothing
    }
    
    self.buttonModeAt = MyButtonIdle;
}

////you will link this to the button's touch up inside
-(IBAction)buttonReleasedInside:(id)sender {
    if (buttonModeAt == MyButtonScanning) {
        ///they released the button and stopped scanning forward
        [self stopScanForward];
    } else if (buttonModeAt == MyButtonStalling) {
        ///they released the button before the delay period finished so we skip forward
        [self skipForward];
    }
    
    self.buttonModeAt = MyButtonIdle;
    
}

その後、ボタンのアクションを IBactions の前のコメントに記載した内容にリンクするだけです。私はこれをテストしていませんが、動作するはずです。

于 2013-03-23T15:48:05.777 に答える