0

我正在尝试设置一个非常简单的滑动识别器,以在滑动时更改文本标签内的文本。我在这里尝试按照苹果的教程进行操作,但显然从未识别过滑动,因为文本永远不会改变。这是我所做的:

首先,我将一个 Swipe Gesture Recognizer 从我的对象库拖到我的视图中,然后将其放在 UIImageView 上。

接下来,我按住 Control 单击将 Swipe Gesture Recognizer 从我的视图底部栏拖到我的 ViewController.h 文件中,如下所示:

#import <UIKit/UIKit.h>
@interface ChordFirstViewController : UIViewController
@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *swiper;
@property (weak, nonatomic) IBOutlet UILabel *sampleText;
@end

然后我在我的.m文件中合成了*swiper,然后定义了一个方法如下:

#import "ChordFirstViewController.h"
@interface ChordFirstViewController ()
@end

@implementation ChordFirstViewController
@synthesize sampleText = _sampleText;
@synthesize swiper = _swiper;

- (IBAction)swiperMethod:(UISwipeGestureRecognizer *)sender {
    if (_swiper.direction == UISwipeGestureRecognizerDirectionLeft) {
        _sampleText.text=@"hi";
    }
}

@end

但是,无论我如何滑动,我都无法更改文本。我添加了一个带有相同 _sampleText.text=@"hi" 消息的按钮,效果很好,但刷卡是不行的。有人可以帮我确定我在这里做错了什么吗?谢谢!

4

1 回答 1

2

出色地。我们可以通过使用方向属性direction来设置。UISwipeGestureRecognizer如果您_swiper.direction == UISwipeGestureRecognizerDirectionLeft每次都这样尝试,那将是错误的,因为在这里您试图找到方向,但它不起作用。

为了识别滑动方向,您可以为不同的方向添加不同的手势。direction property唯一定义allowed directions被识别为滑动的手势,not the actual direction of a particular swipe

 UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];
[leftRecognizer release];

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];
[rightRecognizer release];

编辑:

-(void)handleSwipeFromRight{
NSLog(@"swipe from right");
}

-(void)handleSwipeFromLeft{
NSLog(@"swipe from Left");
}
于 2013-05-19T07:05:39.563 回答