在我的项目中,我想在点击超过3秒时显示隐藏的图像视图。我知道我需要使用NSTimer,但我从未创建过UIImageView触摸事件。如何将TapGestureRecognizer与NSTimer结合使用以实现我想要做的事情?我对触摸iOS中的事件完全不熟悉,我刚刚开始探索这个问题。所以,任何帮助将不胜感激。谢谢!
更新: 我实现了如下的UILongPressGestureRecognizer,但是现在,即使我按下图像外的某个地方,也会出现隐藏的图像。如何仅在按下隐藏图像时才能显示?
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longPress.numberOfTouchesRequired = 1;
longPress.minimumPressDuration = 3;
[self.view addGestureRecognizer:longPress];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
BrokenGlass.hidden = NO;
}
}
答案 0 :(得分:2)
您不需要UITapGestureRecognizer
和计时器,而是需要UILongPressGestureRecognizer
。
答案 1 :(得分:0)
[**self.view** addGestureRecognizer:longPress];
这是我的解决方案:
@interface ViewController () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create the imageView
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
// enable the user interaction in the imageView (otherwise it will not receive events)
_imageView.userInteractionEnabled = YES;
// add as a subview of the main view
[self.view addSubview:_imageView];
// create the gesture recognizer
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longPressGesture.delegate = self;
longPressGesture.minimumPressDuration = 3;
// add the gesture to the imageView
[_imageView addGestureRecognizer:longPressGesture];
}
#pragma mark - UIGestureRecognizerDelegate
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
// show the image
_imageView.image = [UIImage imageNamed:@"cat.jpeg"];
}