#Objective - C - UI-design - 第四天 -UIKit框架-UIKit-事件手势-刮刮乐小游戏
来源:互联网 发布:数据库存储图片路径 编辑:程序博客网 时间:2024/05/16 05:33
响应者
- iOS中所有能响应事件(触摸、晃动、远程事件)的对象都是响应者。
- 系统定义了一个抽象的父类UIResponder来表示响应者。 其子类都是响应者。
阻断响应者链
- 响应者链可以被打断。无法完成检测查询过程。
- 视图类的属性 : userInteractionEnabled ,关闭后能阻断查询过程。
//UIView和Label用户交互默认为NO //页面多控件尤其复合时,容易使touch被阻断覆盖,将其调整为YES以解决问题 //用户交互,可开启或阻断事件 //tv.userInteractionEnabled = NO; //UILabel
各种手势
{//全局变量 int i ; UIImageView *imageView; UIImageView *image;}//手势初始化设置#pragma mark- 准备 image = [[UIImageView alloc]initWithFrame:self.view.bounds]; image.image = [UIImage imageNamed:@"1.jpg"]; imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 150, 150)];//初始化 [imageView setBackgroundColor:[UIColor redColor]]; imageView.alpha = .5; //层级圆角 imageView.layer.masksToBounds = YES; imageView.layer.borderWidth = 5.; imageView.layer.borderColor = [[UIColor blackColor]CGColor]; imageView.layer.cornerRadius = 15.;#pragma mark- 必须打开交互开关 [imageView setUserInteractionEnabled:YES]; [self.view addSubview:image]; [self.view addSubview:imageView];#pragma mark- 轻击,点击(tap) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapse:)];//创建手势识别对象 tap.numberOfTapsRequired = 1; //点击次数 tap.numberOfTouchesRequired = 1; //点击屏幕的手指数 [imageView addGestureRecognizer:tap]; //屏幕换背景 UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapse1:)]; [self.view addGestureRecognizer:tap1];#pragma mark- 捏合,收缩(pinch) UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinse:)]; [imageView addGestureRecognizer:pinch];#pragma mark- 旋转(rotate) UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rose:)]; [imageView addGestureRecognizer:rotate];#pragma mark- 拖动(pan) UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panse:)]; //手指数最大最小值 [pan setMinimumNumberOfTouches:1]; [pan setMaximumNumberOfTouches:1]; [imageView addGestureRecognizer:pan];#pragma mark- 长按(longpress) UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longse:)]; //次数 longPress.numberOfTapsRequired = 1; //手指 longPress.numberOfTouchesRequired = 1; //长按最小时间 [longPress setMinimumPressDuration:.2f]; [imageView addGestureRecognizer:longPress];#pragma mark- 轻扫(swipe) UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)]; //指定划的方向 [swipeUp setDirection:UISwipeGestureRecognizerDirectionUp]; //2 UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)]; [swipeDown setDirection:UISwipeGestureRecognizerDirectionDown]; //3 UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; //4 UISwipeGestureRecognizer *swiperRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipese:)]; swiperRight.direction = UISwipeGestureRecognizerDirectionRight; //放入屏幕 [self.view addGestureRecognizer:swiperRight]; [self.view addGestureRecognizer:swipeUp]; [self.view addGestureRecognizer:swipeLeft]; [self.view addGestureRecognizer:swipeDown];#pragma mark- 摇动(shake) //摇动手势操作方法,点击硬件设备上Hardware的Shake Gesture按钮即可//手势触发时方法#pragma mark- 轻击后实现方法-(void)tapse:(UITapGestureRecognizer *)tap{ if (imageView.backgroundColor == [UIColor redColor]) { [imageView setBackgroundColor:[UIColor cyanColor]]; }else{ imageView.backgroundColor = [UIColor redColor]; }}-(void)tapse1:(UITapGestureRecognizer *)tap{ if (i > 5) { i = 1; } NSString *string = [NSString stringWithFormat:@"%d.jpg",i++]; [image setImage:[UIImage imageNamed:string]];}#pragma mark- 捏合实现方法-(void)pinse:(UIPinchGestureRecognizer *)pin{ if (pin.state == UIGestureRecognizerStateChanged) {//状态改变 //使用仿射变换来放大缩小 CGAffineTransform trans = imageView.transform; trans = CGAffineTransformScale(trans, pin.scale, pin.scale); imageView.transform = trans; } pin.scale = 1.0;//复位}#pragma mark- 旋转(rotate)实现-(void)rose:(UIRotationGestureRecognizer *)rot{ if (rot.state == UIGestureRecognizerStateChanged) { imageView.transform = CGAffineTransformMakeRotation(rot.rotation); }}#pragma mark- 拖动实现-(void)panse:(UIPanGestureRecognizer*)pan{ if (pan.state == UIGestureRecognizerStateChanged) { CGPoint point = [pan translationInView:self.view];//pan相对于屏幕的坐标(移动距离) CGPoint center = imageView.center;//要移动物件的中心坐标 imageView.center = CGPointMake(point.x + center.x, point.y + center.y); } [pan setTranslation:CGPointZero inView:self.view];//复位}#pragma mark- 长按实现-(void)longse:(UILongPressGestureRecognizer *)lp{ if (lp.state == UIGestureRecognizerStateChanged) {//长按时间到变黄 imageView.backgroundColor = [UIColor yellowColor]; } else if(lp.state == UIGestureRecognizerStateEnded){//松开鼠标变回红色 imageView.backgroundColor = [UIColor redColor]; }}#pragma mark- 轻扫实现-(void)swipese:(UISwipeGestureRecognizer *)swipe{ if (swipe.direction == UISwipeGestureRecognizerDirectionUp) { imageView.backgroundColor = [UIColor yellowColor]; }else if (swipe.direction == UISwipeGestureRecognizerDirectionDown){ imageView.backgroundColor = [UIColor cyanColor]; }else if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){ imageView.backgroundColor = [UIColor grayColor]; }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){ imageView.backgroundColor = [UIColor brownColor]; NSLog(@"草那么麻烦"); }}#pragma mark- 摇动(shake)-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ //重写 if (event.subtype == UIEventSubtypeMotionShake) { imageView.image = [UIImage imageNamed:@"5"]; NSLog(@"草泥马"); }}
刮刮乐小游戏
{ UIImageView *girl;//底图 UIImageView *clear;//遮罩图} //底图 girl = [[UIImageView alloc]initWithFrame:self.view.bounds]; [girl setImage:[UIImage imageNamed:@"001.jpg"]]; [self.view addSubview:girl]; //遮罩图 clear = [[UIImageView alloc]initWithFrame:girl.bounds]; [clear setImage:[UIImage imageNamed:@"002.jpg"]]; [self.view addSubview:clear];
//消除实现方法//手势移动时触发-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //图片覆盖清除事件 UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:clear]; UIGraphicsBeginImageContext(clear.frame.size); [clear.image drawInRect:clear.bounds]; CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(point.x, point.y, 50, 50)); clear.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();}
0 0
- #Objective - C - UI-design - 第四天 -UIKit框架-UIKit-事件手势-刮刮乐小游戏
- #Objective - C - UI-design - 第一天 -UIKit框架-UIKit-UIView
- #Objective - C - UI-design - 第二天 - UIKit框架-UIKit-基本控件-相关习题
- #Objective - C - UI-design - 第五天 -UIKit框架-UIKit-Catalog-UIControl
- #Objective - C - UI-design - 第七天 -UIKit框架-UIKit-导航控制器-UINavigationController
- #Objective - C - UI-design - 第三天 -UIKit框架-UIKit-视图控制器-注意:只有开关灯游戏内容
- #Objective - C - UI-design - 第六天 -UIKit框架-UIScrollView-分屏相册练习(相册缩略图变为浏览到第几张)
- Objective-C: UIKit:XIB
- UI-第一讲:UIKit框架-UIKit-UIView
- UIKit框架
- UIKit框架
- UIKit框架
- 轻量级的前端UI开发框架 - UIkit
- Objective-C:: UIKit: 视图控制器、UILabel、UIButton
- Objective-C: UIKit: UIScrollView、欢迎界面
- UIKit 手势识别 UIGestureRecognizer
- iOS UIKit - 手势处理
- UIKit
- iOS navigation (pop)返回按钮点击提示框
- Android 开发中百度地图运行一段时间地图轨迹不更新的问题
- 由ssh框架引起的set里的对象排序问题
- 人见人爱A+B(1)
- Python修改当前工作目录
- #Objective - C - UI-design - 第四天 -UIKit框架-UIKit-事件手势-刮刮乐小游戏
- 说完电调就是螺旋桨了
- 含义while((ch=getchar())!='#'){},while(ch=getchar(),ch!='#'),while(ch=getchar() && ch!='#')
- centos 安装VMwareTools
- 反射(四)使用反射机制调用对象的私有方法及成员变量
- SiourceInght 和 UltraEdit 修改换行符,匹配不同的操作系统
- 返回系统HOME桌面
- 蓝桥杯 最大最小值 (暴力)
- ucgui 窗口管理及回调函数的应用