UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按


#import "RootViewController.h"

@interface RootViewController ()
{
    UIImageView *imageView;
    NSInteger number;//用于存放图片的名字
    BOOL flag;//用于标记当前是哪张图片

}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    //默认值是yes;用于点击事件更改照片
    flag = YES;
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"美女1.png"]];
    imageView.frame = [UIScreen mainScreen].bounds;
    
    //contentMode内容模式
    //UIViewContentModeScaleToFill规模填充
    //UIViewContentModeScaleAspectFit内容符合固定方面。剩余部分是透明的
    //UIViewContentModeScaleAspectFill内容扩展与固定方面填补。可能是剪的一部分内容。
    //UIViewContentModeRedraw重划边界变化(调用-setNeedsDisplay)
    //UIViewContentModeCenter内容保持相同大小。定位调整。
    imageView.contentMode = UIViewContentModeScaleToFill;
    
    
    //UIIamgeView 和 UILabel一样,默认的交互是关闭的NO
    //所以用userInteractionEnabled = YES
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    [imageView release];
    
    
    
    //UISegmentedControl,分段控制器,继承于UIControl
    //参数:参数类型,数组内存放每个分段控制按钮的标题
    NSArray *items = @[@"点击", @"捏合", @"旋转", @"清扫", @"平移", @"边缘移动", @"长按"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
    segmentedControl.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);
    
    //关联方法
    //分段控制器,触发事件是UIControlEventValueChanged//和button的不一样UIControEventTouchUpInside
    [segmentedControl addTarget:self action:@selector(changeGesture:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];
    [segmentedControl release];
    
    
    
    // Do any additional setup after loading the view.
}


- (void)changeGesture:(UISegmentedControl *)aControl
{
    //选中的是第几个区域
    //selectedSegmentIndex, 从0开始依次增加
    NSLog(@"%ld", aControl.selectedSegmentIndex);
    
    //抽象基类的特点:不能够直接使用,必须通过子类化的形式才能使用,换言之,只能使用子类
    //UIGestureRecognizer,手势识别器,其实是对触摸的封装,是抽象基类
    
    //移除之前的手势
    for (UIGestureRecognizer *gesture in imageView.gestureRecognizers) {
        [imageView removeGestureRecognizer:gesture];
    }
    
    
    switch (aControl.selectedSegmentIndex) {
        case 0://单击
        {
            //单击手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesture)];
            //单击次数
            tap.numberOfTapsRequired = 2;
            //需要几个手指同时点击
            tap.numberOfTouchesRequired = 1;
            
            //把手势添加到某个视图上
            [imageView addGestureRecognizer:tap];
            //释放
            [tap release];
            
        
        
        }
            break;
        
        case 1://捏合
        {
            //捏合手势
            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
            [imageView addGestureRecognizer:pinch];
            [pinch release];
        }
            break;
            
        case 2://旋转
        {   //旋转手势
            UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
            [imageView addGestureRecognizer:rotation];
            [rotation release];
        }
            break;
            
        case 3://清扫
            
        {//清扫手势
//            imageView.image = [UIImage imageNamed:@"美女1.png"];
//            imageView.contentMode = UIViewContentModeScaleToFill;
            
            //向左滑动
            UISwipeGestureRecognizer *leftWipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];
            //设置初始值1
            number = 1;
            //设置方向, 左右(只支持一个方向)
            leftWipe.direction = UISwipeGestureRecognizerDirectionLeft;
            [imageView addGestureRecognizer:leftWipe];
            [leftWipe release];
            
            
            //向右滑动
            UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swape:)];
            //设置初始值1
            number = 1;
            //设置方向, 左右(只支持一个方向)
            rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
            [imageView addGestureRecognizer:rightSwipe];
            [rightSwipe release];
         }
            break;
            
        case 4://平移
        {
            //平移手势
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
            [imageView addGestureRecognizer:pan];
            [pan release];
        }
            break;
            
        case 5://边缘移动//扁平化之后才有的
        {
            //注意:边缘移动手势触摸的机制,必须保证视图紧贴屏幕的边缘
            UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan)];
            //边缘(支持一个方向)
            edgePan.edges = UIRectEdgeLeft;
            edgePan.edges = UIRectEdgeRight;
            
            [imageView addGestureRecognizer:edgePan];
            [edgePan release];
        
        }
            break;
            
        case 6://长按
        {
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
            //时间相应(按多长时间)
            longPress.minimumPressDuration = 1.0;
            [imageView addGestureRecognizer:longPress];
            [longPress release];
        
        }
            break;
        default:
            break;
    }

}

#pragma mark ---单击响应方法的实现
/**
 *  单击响应方法的实现
    通过点击实现两个图片的交换显示
 */
- (void)gesture
{
    //1.第一种方法
//    if (flag) {
//        imageView.image = [UIImage imageNamed:@"美女1.png"];
//        imageView.contentMode = UIViewContentModeScaleToFill;
//        flag = NO;
//    } else {
//    
//        imageView.image = [UIImage imageNamed:@"美女2.png"];
//        imageView.contentMode = UIViewContentModeScaleToFill;//图片填充
//        flag = YES;
//        
//    }

    //2.第二种方法,三目运算
    imageView.image = flag ? [UIImage imageNamed:@"美女2.png"] : [UIImage imageNamed:@"美女1.png"];
    flag = !flag;
    

}

#pragma mark ---捏合方法的实现
- (void)pinch:(UIPinchGestureRecognizer *)aPinch
{
    //aPinch.scale缩放比率
    NSLog(@"%.2f", aPinch.scale);
    //图像的变形 transform 是UIView的属性
    
    //图像的变形
    //CGAffineTransformScale(缩放比例, X轴, Y轴)
    //参数1:原始图的transform
    //参数2:X轴上改变的比率
    //参数3:Y轴上改变的比率
    imageView.transform = CGAffineTransformScale(imageView.transform, aPinch.scale, aPinch.scale);
    //重置比率(是为了让图像的比率正常)
    aPinch.scale = 1;


}


#pragma mark ---旋转方法的实现
- (void)rotation:(UIRotationGestureRecognizer *)aRotation
{
    //旋转角度
    NSLog(@"%.2f", aRotation.rotation);
    //改变图片旋转角度
    //参数1:视图原先的transform
    //参数2:变化的角度
    imageView.transform = CGAffineTransformRotate(imageView.transform, aRotation.rotation);
    //重置角度
    aRotation.rotation = 0;


}

#pragma mark ---清扫方法的实现

- (void)swape:(UISwipeGestureRecognizer *)aSwape
{

    if (aSwape.direction == UISwipeGestureRecognizerDirectionLeft) {
        number++;
        if (number == 9) {//当向左滑动9次时,第9次变为从1张图片开始
            number = 1;
        }
    }
    if (aSwape.direction == UISwipeGestureRecognizerDirectionRight) {
        number--;
        if (number == 0) {//本身是1当向右滑动1次时变为0,变为从8张图片开始
            number = 8;
        }
    }
    NSString *name = [NSString stringWithFormat:@"美女%ld.png", number];
    imageView.image = [UIImage imageNamed:name];
}



#pragma mark ---平移方法的实现
- (void)pan:(UIPanGestureRecognizer *)aPan
{
    //手指移动的位置(X轴和Y轴偏移量)
    CGPoint point = [aPan translationInView:imageView];
    NSLog(@"%@", NSStringFromCGPoint(point));
    
//    //更改center偏移量
//    //方法1.
//    CGPoint temp = imageView.center;
//    temp.x += point.x;
//    temp.y += point.y;
//    imageView.center = temp;
//    //重置point
//    [aPan setTranslation:CGPointZero inView:imageView];
//    
    //方法2
    //参数1:图片原有的transform
    //参数2:X轴位移
    //参数3:Y轴位移
    imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
    [aPan setTranslation:CGPointZero inView:imageView];
    
}

#pragma mark ---边缘移动方法的实现
- (void)edgePan
{
    NSLog(@"边缘");

}


#pragma mark ---长按方法的实现

- (void)longPress:(UILongPressGestureRecognizer *)aLongPress
{
    //手势状态
    //aLongPress.state
    if (aLongPress.state == UIGestureRecognizerStateBegan) {
        //屏幕截图
        //绘制一张图片,屏幕大小
        UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        //绘画结束
        UIGraphicsEndImageContext();
        //存到相册中
        UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    }

    //    结束触发
    //    if (aLongpress.state == UIGestureRecognizerStateEnded) {
    //        <#statements#>
    //    }
}

#pragma mark ---屏幕截图提示方法的实现


- (void)               image: (UIImage *) image
    didFinishSavingWithError: (NSError *) error
                 contextInfo: (void *) contextInfo {
    
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片已保存" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alterView show];
    [alterView release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 
 
posted @ 2015-03-26 09:17  Dived  阅读(479)  评论(0编辑  收藏  举报