7

UIButtonのタッチが終了したときに発生するイベントを処理したい。UIControl には、タッチを実装するいくつかのイベント (UIControlEventTouchDown、UIControlEventTouchCancel など) があることを知っています。しかし、 UIControlEventTouchDownUIControlEventTouchUpInside以外はキャッチできません。

私のボタンは、いくつかの UIView のサブビューです。その UIView にはuserInteractionEnabledプロパティがYESに設定されています。

どうしたの?

4

6 に答える 6

25

に従って、ボタンの「アクション ターゲット」を設定できます。ControlEvents

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

例:

[yourButton addTarget:self 
           action:@selector(methodTouchDown:)
 forControlEvents:UIControlEventTouchDown];

[yourButton addTarget:self 
           action:@selector(methodTouchUpInside:)
 forControlEvents: UIControlEventTouchUpInside];

-(void)methodTouchDown:(id)sender{

   NSLog(@"TouchDown");
}
-(void)methodTouchUpInside:(id)sender{

  NSLog(@"TouchUpInside");
}
于 2013-03-27T19:32:40.310 に答える
3

を拡張する独自のカスタム クラスを作成する必要がありますUIButton。ヘッダー ファイルは次のようになります。

@interface customButton : UIButton
{
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

次に、実装ファイルを作成します

于 2013-03-27T19:42:52.533 に答える
0

もっと簡単だと思う

UILongPressGestureRecognizer *longPressOnButton = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressOnButton:)];
longPressOnButton.delegate = self;
btn.userInteractionEnabled = YES;
[btn addGestureRecognizer:longPressOnButton];



- (void)longPressOnButton:(UILongPressGestureRecognizer*)gesture
{
    // When you start touch the button
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
       //start recording
    }
    // When you stop touch the button
    if (gesture.state == UIGestureRecognizerStateEnded)
    {
        //end recording
    }
}
于 2017-01-05T08:08:11.087 に答える
0

TouchDown:イベントとプライマリ アクションを使用して UIButton の IBOutlets を追加するだけです。Triggered:

- (IBAction)touchDown:(id)sender {
    NSLog(@"This will trigger when button is Touched");
}

- (IBAction)primaryActionTriggered:(id)sender {
    NSLog(@"This will trigger Only when touch end within Button Boundary (not Frame)");
}
于 2017-01-09T08:47:05.753 に答える
0

スウィフト 3.0 バージョン:

 let btn = UIButton(...)

 btn.addTarget(self, action: #selector(MyView.onTap(_:)), for: .touchUpInside)

 func onTap(_ sender: AnyObject) -> Void {

}
于 2017-04-04T08:35:52.687 に答える