我想提供UIButton
个不同的功能,具体取决于它是按下和释放一次,还是按住(理想情况下为1.5秒)并在屏幕上移动。我目前正在使用此代码:
[button addTarget:self action:@selector(open:)
forControlEvents:UIControlEventTouchDown]
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[button addGestureRecognizer:panRecognizer];
-(IBAction)open:(id)sender {}
-(void)move:(id)sender{}
move:
工作正常,但open:
没有。
答案 0 :(得分:0)
也许你可以使用按钮的拖动事件,比如
[button addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
您可以查看以下链接:http://www.cocoanetics.com/2010/11/draggable-buttons-labels/
你可以在移动方法中放置一个标志,当输入touch up方法时,你检查按钮是否已移动,以决定触发打开相关逻辑
#define BUTTON_DRAGGED_TAG -100
#define BUTTON_DEFAULT_TAG 0
- (void)onButtonTouchUpInside:(UIButton *)button withEvent:(UIEvent *)event
{
if (button.tag != BUTTON_DRAGGED_TAG)
{
//doOpen
}
button.tag = BUTTON_DEFAULT_TAG;
}
- (void)onButtonTouchUpOutside:(UIButton *)button withEvent:(UIEvent *)event
{
button.tag = BUTTON_DEFAULT_TAG;
}
- (void)onButtonTouchCancel:(UIButton *)button withEvent:(UIEvent *)event
{
button.tag = BUTTON_DEFAULT_TAG;
}
- (void)onButtonDraged:(UIButton *)button withEvent:(UIEvent *)event
{
button.tag = BUTTON_DRAGGED_TAG;
}
答案 1 :(得分:0)
我这样做了,让我知道它是否适合你:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTapGesture:)];
tapGesture.cancelsTouchesInView = NO;
tapGesture.delegate = self;
[tapGesture requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];
[self.scrollView addGestureRecognizer:tapGesture];
self.tapGesture = tapGesture;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view == self.button)
{
return NO;
}
return YES;
}
- (void)didRecognizeTapGesture:(UITapGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded)
{
NSLog(@"GESTURE ENDED");
}
}
- (void)didPressButton:(UIButton*)sender
{
NSLog(@"BUTTON TOUCH UP INSIDE");
}