ios 图片旋转放大加移动

今天这更有点迟了,让大家久等了。哈哈,今天好玩的又来了,我们来玩一玩,怎么把一张照片,旋转放大加移动。很期待吧,那么事不宜迟我们来实现它们。


说到这些功能,在ios中实现无非就是手势触摸功能,加上计算偏移,算出坐标。就ok了。非常的简单。


我们新建一个工程

#import "ViewController.h"


@interface ViewController ()<UIGestureRecognizerDelegate>

{

    UIImageView *_imageView;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    

    [self createImageView];

    [self createPinchGesture];

    [self createRotataeGesture];

//    [self createPanGesture];

}


//创建缩放手势

-(void)createPinchGesture

{

    

    UIPinchGestureRecognizer *pinchGes  =[[UIPinchGestureRecognizer alloc]init];

    

    pinchGes .delegate = self;

    

    [pinchGes addTarget:self action:@selector(pinchGes:)];

    [_imageView addGestureRecognizer:pinchGes];

}


-(void)pinchGes:(UIPinchGestureRecognizer *)ges

{

    //比列

    CGFloat scale = ges.scale;

    //改变视图比列

    ges.view.transform = CGAffineTransformScale(ges.view.transform, scale, scale);

    //比列为1

    ges.scale = 1;

}


//创建旋转手势

-(void)createRotataeGesture

{

    UIRotationGestureRecognizer *rotationGes = [[UIRotationGestureRecognizer alloc]init];

    

    rotationGes.delegate = self;

    

    [rotationGes addTarget:self action:@selector(rotGes:)];

    [_imageView addGestureRecognizer:rotationGes];

}

-(void)rotGes:(UIRotationGestureRecognizer *)ges

{

    

    ges.view.transform = CGAffineTransformRotate(ges.view.transform, ges.rotation);

    ges.rotation = 0;

    

}

-(void)createImageView

{

    UIImageView *imageView = [[UIImageView alloc]init];

    

    imageView.frame = CGRectMake(30, 60, 330, 500);

    imageView.image = [UIImage imageNamed:@"webwxgetmsgimg.jpg"];

    

    imageView.userInteractionEnabled = YES;

    

    [self.view addSubview:imageView];

    _imageView = imageView;

}


//计算偏移量移动图片

//第一种计算方式

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    [[self class] cancelPreviousPerformRequestsWithTarget:self];

    

    //取到当前触摸对象

    UITouch *touch = [touches anyObject];

    

    //当前位置

    CGPoint point = [touch locationInView:self.view];

    //之前的位置

    CGPoint prePoint = [touch previousLocationInView:self.view];

    

    //计算两次点击的偏移量

    CGPoint trans = CGPointMake(point.x - prePoint.x, point.y - prePoint.y);

    

    //修改imageview的结构(imageview的中心点 赋给center ,就是坐标的变化)

    CGPoint center = _imageView.center;

    //把计算出来的偏移量,就是imagviewx轴加上算出来的偏移量,如是y轴不变,那就是0,不用加

    _imageView.center = CGPointMake(center.x + trans.x, center.y+ trans.y);

    

    //这个是点击事件,因为都已经经过准确的计算,每次都有精确的偏移量所以不需要清理上次的偏移量

    

    

}


//2种计算方式

//-(void)createPanGesture

//{

//    UIPanGestureRecognizer *panGse = [[UIPanGestureRecognizer alloc]init];

//    

//    panGse.delegate = self;

//  

//    [panGse addTarget:self action:@selector(panGes1:)];

//    [_imageView addGestureRecognizer:panGse];

//}

//-(void)panGes1:(UIPanGestureRecognizer *)ges

//{

//    CGPoint point = [ges translationInView:ges.view];

//    

//    ges.view.transform = CGAffineTransformTranslate(ges.view.transform, point.x, point.y);

//    

//    //偏移量清零(这个事手势,如果每次手势调用都不清理会自动累加到下次,造成不精确,所以每次都要清理上次的偏移量,重新计算下次的,保证精确)这个没有经过准确的计算


////就是回到原先位置,然后从新计算,不然会累积到下次来

//    [ges setTranslation:CGPointZero inView:ges.view];

//    

//    

//}

//和其他手势一起进行

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

@end

技术分享

技术分享


技术分享

没错,这就是本公子了,哈哈。今天到此结束,大年30休息,(提前说下)就不更了,祝大家过个好年。


技术分享


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。