Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何使用两种不同的方法来滑动按钮和触碰内部?_Iphone_Objective C_Cocoa Touch_Uibutton - Fatal编程技术网

Iphone 如何使用两种不同的方法来滑动按钮和触碰内部?

Iphone 如何使用两种不同的方法来滑动按钮和触碰内部?,iphone,objective-c,cocoa-touch,uibutton,Iphone,Objective C,Cocoa Touch,Uibutton,我有一个视图,其中有几个大按钮,设置如下: SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55, 0, 300, 50)] autorelease]; btn.tag = k; [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"titleBackground.png"]]

我有一个视图,其中有几个大按钮,设置如下:

SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55, 0, 300, 50)] autorelease];    
btn.tag = k;

[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"titleBackground.png"]] 
                                                              forState:UIControlStateNormal];
[btn   addTarget:self
          action:@selector(indexAction:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"picture_0.png"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake((applicationFrame.size.width - image.size.width)/2, applicationFrame.size.height/2, image.size.width, image.size.height)];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
我希望能够触摸这些按钮,然后启动indexAction方法,或者——如果识别出刷卡——启动另一种方法

我想我将UIButton子类化,让用户通过刷卡,但这并不是一个真正的解决方案,因为现在刷卡和点击都被识别,所以这两种方法都会启动

有办法解决这个问题吗?我如何确定刷卡的优先级,并且仅在没有刷卡的情况下允许触碰内部

任何帮助都将不胜感激

下面是我如何将UIButton子类化的:

#import "SwipeButton.h"

@implementation SwipeButton

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.superview touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesMoved:touches withEvent:event];
    [self.superview touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.superview touchesEnded:touches withEvent:event];

} 
@end

看一看和相关的示例()。

看一看和相关的示例()。

我认为您不需要对UIButton类进行子类化。你为什么不试试这样的东西:

SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55, 0, 300, 50)] autorelease];    
btn.tag = k;

[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"titleBackground.png"]] 
                                                              forState:UIControlStateNormal];
[btn   addTarget:self
          action:@selector(indexAction:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"picture_0.png"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake((applicationFrame.size.width - image.size.width)/2, applicationFrame.size.height/2, image.size.width, image.size.height)];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
然后创建处理轻触和滑动按钮的方法:

// Prevent recognizing touches on the slider
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

if ([touch.view isKindOfClass:[UIButton class]]) {
    return YES;
}
return NO;
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

if ([recognizer direction] == UISwipeGestureRecognizerDirectionLeft) {

    // Handle left swipe
    NSLog(@"left swipe");

} else {

    // Handle right swipe
    NSLog(@"right swipe");

}
}

- (void)buttonTapped:(id)sender {

NSLog(@"button pressed");

}
前面三个方法中的第一个是在协议中声明的,所以不要忘记说您的类正在实现该协议:

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
@接口MyViewController:UIViewController

它很管用,我自己就试过了。希望能有帮助!;)

我认为您不需要将UIButton类作为子类。你为什么不试试这样的东西:

SwipeButton* btn = [[[SwipeButton alloc] initWithFrame:CGRectMake(55, 0, 300, 50)] autorelease];    
btn.tag = k;

[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"titleBackground.png"]] 
                                                              forState:UIControlStateNormal];
[btn   addTarget:self
          action:@selector(indexAction:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"picture_0.png"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake((applicationFrame.size.width - image.size.width)/2, applicationFrame.size.height/2, image.size.width, image.size.height)];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[recognizer setDelegate:self];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
然后创建处理轻触和滑动按钮的方法:

// Prevent recognizing touches on the slider
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

if ([touch.view isKindOfClass:[UIButton class]]) {
    return YES;
}
return NO;
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

if ([recognizer direction] == UISwipeGestureRecognizerDirectionLeft) {

    // Handle left swipe
    NSLog(@"left swipe");

} else {

    // Handle right swipe
    NSLog(@"right swipe");

}
}

- (void)buttonTapped:(id)sender {

NSLog(@"button pressed");

}
前面三个方法中的第一个是在协议中声明的,所以不要忘记说您的类正在实现该协议:

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
@接口MyViewController:UIViewController

它很管用,我自己就试过了。希望能有帮助!;)

你试过这门课吗?@Nick Weaver:谢谢,Nick。我没有使用UIGestureRecognizer,因为我想定制刷卡需要多长时间。因此,我使用-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event。。。但我想使用手势识别器也没什么害处。为什么要投反对票?我想这是一个愚蠢的问题,但事实上,我努力想了想。你上过这门课吗?@Nick Weaver:谢谢,Nick。我没有使用UIGestureRecognizer,因为我想定制刷卡需要多长时间。因此,我使用-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event。。。但我想使用手势识别器也没什么害处。为什么要投反对票?我想这是一个愚蠢的问题,但事实上,我努力想了想。非常感谢。。。!真的很有帮助。我没有考虑使用手势识别器,因为我使用-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)事件来确定刷卡长度(并根据需要定制)。但很明显,我想,两者都用并没有什么错…就像一个符咒。。。再次感谢你。但我真的不知道如何设置所需的最小滑动长度。理想情况下,我希望将其设置为10像素,将方差设置为15像素,但我看不到使用UIGestureRecognitizer实现这一点的方法。如果使用UIWipeGestureRecognitizer,则无法设置最小滑动长度。你为什么需要这样的东西呢?我希望刷卡是非常有反应的。例如,使用照片应用程序。如果你只滑动手指一点点(约5px),图片就会移动。如果您希望获得即时反馈,我想定制所需的刷卡长度将非常有用。我有这样一种方法,但正如我所说的,我正在使用touchesend,然后测量触摸的起点和终点之间的差异——从而确定是否达到了所需的滑动长度阈值。非常感谢。。。!真的很有帮助。我没有考虑使用手势识别器,因为我使用-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)事件来确定刷卡长度(并根据需要定制)。但很明显,我想,两者都用并没有什么错…就像一个符咒。。。再次感谢你。但我真的不知道如何设置所需的最小滑动长度。理想情况下,我希望将其设置为10像素,将方差设置为15像素,但我看不到使用UIGestureRecognitizer实现这一点的方法。如果使用UIWipeGestureRecognitizer,则无法设置最小滑动长度。你为什么需要这样的东西呢?我希望刷卡是非常有反应的。例如,使用照片应用程序。如果你只滑动手指一点点(约5px),图片就会移动。如果您希望获得即时反馈,我想定制所需的刷卡长度将非常有用。我有这样一种方法,但正如我所说的,我使用touchesend,然后测量触摸的起点和终点之间的差异——从而确定是否达到了所需的滑动长度阈值。