我正在尝试设置一个非常简单的滑动识别器,以便在滑动时更改文本标签内的文本。我尝试按照苹果的教程here进行操作,但是滑动显然从未被识别过,因为文本永远不会改变。这是我做的:
首先,我将一个Swipe Gesture Recognizer从我的对象库拖到我的视图中,并将其放在UIImageView上。
接下来,我按住Control键点击将滑动手势识别器从我视图的底栏拖到我的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”消息的按钮,并且工作正常,但是滑动是不行的。有人可以帮我确定我在这里做错了什么吗?谢谢!
答案 0 :(得分:2)
好。我们可以使用direction属性将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");
}