【发布时间】:2011-07-12 06:24:50
【问题描述】:
如何创建具有两个操作的 UIButton。
我知道通过使用 UILongPressGestureRecognizer 我们可以执行长按。
但我的要求是,当我长按 UIButton 时,它必须执行一个动作并且在触摸时
在里面,它必须执行另一个动作。
谢谢。
下面是我的代码。
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");
}
【问题讨论】:
标签: cocoa-touch uibutton