一,响应事件
1 我们的程序只要运行,只要不是人为的停止程序我们的程序一直处于运行状态
2 程序在运行的时候会一直检测程序的任何响应事件,无论是点击事件,触摸事件还是晃动事件等等一切行为都会被检测
3 分为两种任务
1 检测事件:UIApplication —>UIWidow —>UIViewController —>UIView —>子视图
2 执行事件:子视图 —>UIView —>UIViewController —>UIWindow — >UIApplication
二 七大手势
七种手势的父类全部都是UIGestureRecognizer ,继承于NSObject
1.轻拍手势(点击)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//设置至少点击几次、
tap.numberOfTapsRequired = 2;
//设置几个手指点击
tap.numberOfTouchesRequired = 2;
//试图添加了轻拍手势,一个手势对象只能够被添加到一个视图上不能添加到多个视图上,如果添加到多个视图上只有最后一个添加这个手势的视图才会拥有这个手势
[self.imageView addGestureRecognizer:tap];//注意在创建imageView视图对象后要将交互打开,才能接收事件交互
2.轻扫手势
UISwipeGestureRecognizer *swipe =
//设置轻扫的方向
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.imageView addGestureRecognizer:swipe];
3.长按手势
UILongPressGestureRecognizer *longPress=
//设置长按时间
longPress.minimumPressDuration = 3;
[self.imageView addGestureRecognizer:longPress];
4.平移手势
UIPanGestureRecognizer *pan =
[self.imageView addGestureRecognizer:pan];
5.旋转手势
UIRotationGestureRecognizer *rotation =
[self.imageView addGestureRecognizer:swipe];
6.缩放手势
UIPinchGestureRecognizer *pinch =
[self.imageView addGestureRecognizer:pinch];
7.屏幕边缘轻扫手势
//屏幕边缘轻扫手势需满足两个条件1.视图必须要和视图的边缘贴紧 2.必须设置轻扫的方向
UIScreenEdgePanGestureRecognize *screen =
//设置轻扫的方向
screen.edges = UIRectEdgeLeft;
[self.imageView addGestureRecognizer:screen];
//////手势的实现
#pragma mark ---边缘轻扫手势---
- (void)edgeAction:(UIScreenEdgePanGestureRecognizer *)edge
{
if(edge.edges == UIRectEdgeLeft)
{
NSLog(@"屏幕边缘轻扫");
}
}
#pragma mark ---缩放手势---
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"缩放");
// 面积被改变之后 下一次再改变面积的时候 要根据上一次的面积继续改变
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
// 将新的面积设置成1
pinch.scale = 1;
}
#pragma mark ---旋转手势---
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋转");
// 旋转 同样也是为了改变视图的transform
// 旋转之后的位置 同样也要变成老的位置 在下一次旋转的时候 要根据上一次旋转的角度继续旋转
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
// 将旋转之后的位置 设置成0
rotation.rotation = 0;
}
#pragma mark ---平移手势----
- (void)panAction:(UIPanGestureRecognizer *)pan
{
NSLog(@"平移");
// 获取手指触摸的点
CGPoint point = [pan translationInView:self.imageView];
// transform 是视图的一个重要属性
// 我们现在所学的手势 以及之后 所学的动画 改变的全部都是视图的transform
// transform可以改变视图的frame 形状以及大小
// 我们的平移 旋转 缩放 手势 全部都是利用transform 最终transform取决于 赋值符号右边调用的API方法(线性代数)
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
// 将新的位置 置成原点(零点)
// 视图移动到新的位置之后 在下一次移动的时候 要再上一次移动的基础上 继续移动
[pan setTranslation:CGPointZero inView:self.imageView];
}
#pragma mark ---长按手势action方法----
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 长按手势 有很多种状态 我们一般情况下使用长按手势的时候 都会和began这个状态结合 只让他触发一次
if (longPress.state == UIGestureRecognizerStateBegan)
{
if (longPress.minimumPressDuration == 3)
{
NSLog(@"我是长按手势 请按住3秒");
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"保存图片" otherButtonTitles:@"相册",@"相机", nil];
// 用来展示
[actionSheet showInView:self.view];
[actionSheet release];
}
}
}
#pragma mark ---UIActionSheet代理方法---
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(@"保存图片");
// 将一个UIImage保存到 系统的相册当中
UIImageWriteToSavedPhotosAlbum(self.imageView.image,
nil, nil, nil);
}
else if (buttonIndex == 1)
{
NSLog(@"相册");
// 先判断有没有系统相册 防止崩溃
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
// 类型:类型决定了 展示出来的是什么(相机还是相册 相册又分为两种形式展示)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 是否可编辑 默认是NO
picker.allowsEditing = YES;
// 设置代理
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
NSLog(@"没有相册");
}
}
else if (buttonIndex == 2)
{
// 判断有没有相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
// 类型:类型决定了 展示出来的是什么(相机还是相册 相册又分为两种形式展示)
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// 是否可编辑 默认是NO
picker.allowsEditing = YES;
// 设置代理
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
NSLog(@"没有相机");
}
}
}
#pragma mark --- 执行拍摄完成之后或者选择完成照片之后自动调用的代理方法---
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"OK");
// 返回到自己的界面
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@",info);
// 字典中对应的这个key 值 就是我们所选择的图片
// 所选择的图片 赋值给 我们自己的imageView,image
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
#pragma mark ---轻扫手势action方法----
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
if(swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"向右轻扫");
}
}
#pragma mark ---轻拍手势action方法----
- (void)tapAction:(UITapGestureRecognizer *)tap
{
// 当参数类型是父类的时候 我们如果想使用 需要强转到子类类型(精确到是哪一种手势)
// UITapGestureRecognizer *t = (UITapGestureRecognizer *)tap;
// tap.view 相当于 self.imageView
// 手势 加在哪个视图上了 tap.view 就相当于谁
if (tap.numberOfTapsRequired == 2)
{
tap.view.frame = CGRectMake(0, 0, 100, 100);
}
}