本文主要是介绍ios开发——手势识别(Long Press),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#import "ViewController.h"@interface ViewController (){//设置垃圾桶是否为空变量,NO为满,YES为空BOOL boolTrashEmptyFlag;
}
//定义两张图和它们容器的属性
@property (strong,nonatomic) UIImage *imageTrashFull;
@property (strong,nonatomic) UIImage *imageTrashEmpty;
@property (strong,nonatomic) UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//界面设置CGRect screen = [[UIScreen mainScreen] bounds];CGFloat imageViewWidth = 128;CGFloat imageViewHeight = 128;CGFloat imageViewTopView = 148;CGRect frame = CGRectMake((screen.size.width - imageViewWidth)/2 , imageViewTopView, imageViewWidth, imageViewHeight);self.imageView = [[UIImageView alloc] initWithFrame:frame];self.imageTrashFull = [UIImage imageNamed:@"Blend Trash Full.png"];self.imageTrashEmpty = [UIImage imageNamed:@"Blend Trash Empty"];self.imageView.image = self.imageTrashFull;[self.view addSubview:self.imageView];//新建手势识别器UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(foundLongPress:)];//设置手势识别器的属性1允许移动位置2最小长按时间recognizer.allowableMovement = 100.0f;recognizer.minimumPressDuration = 1.0;//将手势识别器关联到imageView[self.imageView addGestureRecognizer:recognizer];//将imageView开启用户事件self.imageView.userInteractionEnabled = YES;
}-(void)foundLongPress:(UIGestureRecognizer*)sender{if (sender.state == UIGestureRecognizerStateBegan) {if (boolTrashEmptyFlag) {self.imageView.image = self.imageTrashFull;boolTrashEmptyFlag = NO;}else{self.imageView.image = self.imageTrashEmpty;boolTrashEmptyFlag = YES;}}
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end
这篇关于ios开发——手势识别(Long Press)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!