长按手势发送按钮

时间:2014-12-07 12:00:40

标签: ios objective-c xcode

如何在长按手势中发送按钮名称,标签或文字?我需要知道长期使用的按钮名称。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setTagS:[[ToalData objectAtIndex:i] objectAtIndex:4]];
    [button addTarget:self action:@selector(siteButtonPressed:)  forControlEvents:UIControlEventTouchUpInside];

    ///For long//
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self
                                               action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 1.0;
    [button addGestureRecognizer:longPress];

    ////


    NSString *string1 = [NSString stringWithFormat:@"%@", [[ToalData objectAtIndex:i] objectAtIndex:1]];
    [button setTitle:string1 forState:UIControlStateNormal];
    button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);

    [self.view addSubview:button];


(void)handleLongPress:(UILongPressGestureRecognizer*)sender
{

if (sender.state == UIGestureRecognizerStateEnded)
{
    NSLog(@"UIGestureRecognizerStateEnded");

}
else if (sender.state == UIGestureRecognizerStateBegan){
    NSLog(@"UIGestureRecognizerStateBegan");




       }

}

1 个答案:

答案 0 :(得分:3)

每个手势识别器都附加到"view",这是手势识别器上的属性。

因此,在“handleLongPress”方法中,您可以执行以下操作:

UIButton *button = (UIButton *)sender.view;

NSLog(@"title is %@", button.titleLabel.text);
NSLog(@"tag is %d", button.tag);