gpt4 book ai didi

cocoa-touch - UIbutton 长按和里面的 Touch Up

转载 作者:行者123 更新时间:2023-12-04 02:47:31 29 4
gpt4 key购买 nike

如何使用两个 Action 创建 UIButton。

我知道通过使用 UILongPressGestureRecognizer 我们可以执行 Longpress。

但我的要求是,当我长按 UIButton 时,它必须执行一个 Action ,并且在触摸时

在里面,它必须执行另一个 Action 。

谢谢。

下面是我的代码。

       UIImage *redImage = [UIImage imageNamed:@"TabFav2.png"];
tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);
redTAb = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];
[tabRedbutton addTarget:self action:@selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];



longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[longpressGesture1 setDelegate:self];
longpressGesture1.cancelsTouchesInView = NO;
[tabRedbutton addGestureRecognizer:longpressGesture1];

[longpressGesture1 release];

- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {

if (longpressGesture.state == UIGestureRecognizerStateBegan)
{
NSlog(@"Long press");
}

}


-(void)redbottonmethod
{

NSlog(@"single tapped");
}

最佳答案

对于点击,您可以使用 UIButton 的“addTarget:...”方法,对于长按,您可以添加手势识别器:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100.0, 100.0, 100.0, 20.0);
[btn setTitle:@"Test" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(userTapped:) forControlEvents:UIControlEventTouchUpInside];

UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] init];
[gr addTarget:self action:@selector(userLongPressed:)];
[btn addGestureRecognizer:gr];
[gr release];

[self.view addSubview:btn];

当然,您需要实现将被调用的 2 个方法:
- (void)userTapped:(id)sender {
NSLog(@"user tapped");
}

- (void)userLongPressed:(id)sender {
NSLog(@"user long pressed");
}

希望有帮助。

==========

编辑:您似乎将按钮用作 UIToolbar 内的 BarButtonItem。所以我改变了我的代码来做同样的事情:
- (void)viewDidLoad {
[super viewDidLoad];

// set up the button
UIImage *redImage = [UIImage imageNamed:@"TabFav2.png"];
UIButton *tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
tabRedbutton.backgroundColor = [UIColor redColor];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);

// set up a bar button item with the button as its view
UIBarButtonItem *redTab = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];

// set up toolbar and add the button as a bar button item
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 100.0, 768.0, 40.0)];
toolbar.barStyle = UIBarStyleBlack;
NSArray *items = [NSArray arrayWithObject:redTab];
[toolbar setItems:items];
[self.view addSubview:toolbar];
[toolbar release];

// add tap handler to button for tap
[tabRedbutton addTarget:self action:@selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];

// add gesture recognizer to button for longpress
UILongPressGestureRecognizer *longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1 release];
}

以及被调用的两个方法:
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(@"Long press");
}


-(void)redbottonmethod {
NSLog(@"single tapped");
}

这段代码绝对有效。

顺便说一句:我注意到在您调用的 2 个方法中的代码中,您有错字:您必须使用 NSLog() 而不是 NSlog()。这可能是问题吗?

关于cocoa-touch - UIbutton 长按和里面的 Touch Up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6660282/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com