017旋转和缩放视图

效果如下:

ViewController.h

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface ViewController : UIViewController {
 4     @private
 5     UIImageView *imgVTransform;
 6     CGFloat rotate;
 7     CGFloat scale;
 8     BOOL isInvert;
 9 }
10 
11 @end

ViewController.m

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)rotateDidPush;
  5 - (void)bigDidPush;
  6 - (void)smallDidPush;
  7 - (void)invertDidPush;
  8 - (void)transformWithAnimation;
  9 @end
 10 
 11 @implementation ViewController
 12 
 13 - (void)viewDidLoad {
 14     [super viewDidLoad];
 15     rotate = 0.0;
 16     scale = 1.0;
 17     isInvert = NO;
 18     
 19     //追加ImageView图片视图
 20     UIImage *img = [UIImage imageNamed:@"Cat.jpg"];
 21     imgVTransform = [[UIImageView alloc] initWithImage:img];
 22     imgVTransform.frame = CGRectMake(0, 0, self.view.frame.size.width-20, 280);
 23     CGPoint newPoint = self.view.center;
 24     newPoint.y -= 20;
 25     imgVTransform.center = newPoint;
 26     imgVTransform.layer.masksToBounds = YES;
 27     imgVTransform.layer.cornerRadius = 8.0;
 28     imgVTransform.layer.borderColor = [UIColor grayColor].CGColor;
 29     imgVTransform.layer.borderWidth = 2.0;
 30     [self.view addSubview:imgVTransform];
 31     //追加4个Button按钮;表示“旋转”、“放大”、“缩小”、“反转”
 32     UIButton *btnRotate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 33     btnRotate.frame = CGRectMake(0, 0, 50, 40);
 34     newPoint.x -= 100;
 35     newPoint.y = self.view.frame.size.height - 70;
 36     btnRotate.center = newPoint;
 37     btnRotate.backgroundColor = [UIColor orangeColor];
 38     [btnRotate setTitle:@"旋转" forState:UIControlStateNormal];
 39     [btnRotate addTarget:self action:@selector(rotateDidPush) forControlEvents:UIControlEventTouchUpInside];
 40     [self.view addSubview:btnRotate];
 41     
 42     UIButton *btnBig = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 43     btnBig.frame = CGRectMake(0, 0, 50, 40);
 44     newPoint.x += 60;
 45     btnBig.center = newPoint;
 46     btnBig.backgroundColor = [UIColor lightGrayColor];
 47     [btnBig setTitle:@"放大" forState:UIControlStateNormal];
 48     [btnBig addTarget:self action:@selector(bigDidPush) forControlEvents:UIControlEventTouchUpInside];
 49     [self.view addSubview:btnBig];
 50     
 51     UIButton *btnSmall = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 52     btnSmall.frame = CGRectMake(0, 0, 50, 40);
 53     newPoint.x += 60;
 54     btnSmall.center = newPoint;
 55     btnSmall.backgroundColor = [UIColor yellowColor];
 56     [btnSmall setTitle:@"缩小" forState:UIControlStateNormal];
 57     [btnSmall addTarget:self action:@selector(smallDidPush) forControlEvents:UIControlEventTouchUpInside];
 58     [self.view addSubview:btnSmall];
 59     
 60     UIButton *btnInvert = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 61     btnInvert.frame = CGRectMake(0, 0, 50, 40);
 62     newPoint.x += 60;
 63     btnInvert.center = newPoint;
 64     btnInvert.backgroundColor = [UIColor greenColor];
 65     [btnInvert setTitle:@"反转" forState:UIControlStateNormal];
 66     [btnInvert addTarget:self action:@selector(invertDidPush) forControlEvents:UIControlEventTouchUpInside];
 67     [self.view addSubview:btnInvert];
 68 }
 69 
 70 - (void)didReceiveMemoryWarning {
 71     [super didReceiveMemoryWarning];
 72     // Dispose of any resources that can be recreated.
 73 }
 74 
 75 #pragma mark - Private Methods
 76 - (void)rotateDidPush {
 77     //以90度为单位旋转
 78     rotate += 90.0;
 79     if (rotate > 359.0) {
 80         rotate = 0.0;
 81     }
 82     [self transformWithAnimation];
 83 }
 84 
 85 - (void)bigDidPush {
 86     //以0.1为单位放大
 87     scale += 0.1;
 88     [self transformWithAnimation];
 89 }
 90 
 91 - (void)smallDidPush {
 92     //以0.1为单位缩小
 93     scale -= 0.1;
 94     if (scale < 0.1) {
 95         scale = 0.1;
 96     }
 97     [self transformWithAnimation];
 98 }
 99 
100 - (void)invertDidPush {
101     //左右反转
102     isInvert = !isInvert;
103     [self transformWithAnimation];
104 }
105 
106 - (void)transformWithAnimation {
107     [UIView beginAnimations:nil context:NULL];
108     
109     CGAffineTransform transformRotate = CGAffineTransformMakeRotation(rotate * ( M_PI / 180.0));
110     CGAffineTransform transformScale = CGAffineTransformMakeScale(scale, scale);
111     CGAffineTransform transformAll = CGAffineTransformConcat(transformRotate, transformScale);
112     if (isInvert) {
113         transformAll = CGAffineTransformScale(transformAll, -1.0, 1.0);
114     }
115     imgVTransform.transform = transformAll;
116     [UIView commitAnimations];
117 }
118 
119 @end

 

posted @ 2015-06-14 23:16  KenmuHuang  阅读(231)  评论(0编辑  收藏  举报
如果您看完本篇博文,觉得对您有所收获,请点击右下角的 [推荐]
如果您想转载,请注明出处(原创内容,请尊重个人劳动成果)
如果您有任何意见或建议,欢迎留言
感谢您的阅读,敬请关注我的后续博客文章