1.说明
UIView的属性transform属性翻转或放缩视图
2.实例
起:在屏幕上方设置UIImageVIew区域,此区域显示一副图片。下方设置4个按钮,分别是旋转、扩大、缩小、反转
终:4个按钮对应4个方法
- (void)rotateDidPush 以90度为单位旋转
- (void)bigDidPush 以0.1为单位扩大
- (void)smallDidPush 以0.1为单位缩小
- (void)invertDidPush 左右反转
.h
#import <UIKit/UIKit.h>
@interface UIKitPrjFrame : UIViewController {
@private
UIView * _imageView;
CGFloat _rotate;
CGFloat _scale;
bool _needFlip;
}
@end
.m
#import "UIKitPrjFrame.h"
@interface UIKitPrjFrame ()
- (void)rotateDidPush;
- (void)bigDidPush;
- (void)smallDidPush;
- (void)invertDidPush;
- (void)transformWithAnimation;
@end
@implementation UIKitPrjFrame
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_rotate = 0.0;
_scale = 1.0;
_needFlip = NO;
self.view.backgroundColor = [UIColor blackColor];
NSString * path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"five.jpg"];
UIImage * image = [[UIImage alloc] initWithContentsOfFile:path];
_imageView = [[UIImageView alloc] initWithImage:image];
CGPoint newPoint = self.view.center;
newPoint.y = self.view.center.y - 60;
_imageView.center = newPoint;
[self.view addSubview:_imageView];
UIButton *rotateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
rotateButton.frame = CGRectMake(0, 0, 50, 40);
newPoint = self.view.center;
newPoint.x -= 75;
newPoint.y = self.view.frame.size.height - 70;
rotateButton.center = newPoint;
[rotateButton setTitle:@"旋转" forState:UIControlStateNormal];
[rotateButton addTarget:self action:@selector(rotateDidPush) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rotateButton];
UIButton *bigButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bigButton.frame = rotateButton.frame;
newPoint = self.view.center;
newPoint.x -= 25;
newPoint.y = self.view.frame.size.height - 70;
bigButton.center = newPoint;
[bigButton setTitle:@"扩大" forState:UIControlStateNormal];
[bigButton addTarget:self action:@selector(bigDidPush) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bigButton];
UIButton *smallButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
smallButton.frame = rotateButton.frame;
newPoint = self.view.center;
newPoint.x += 25;
newPoint.y = self.view.frame.size.height - 70;
smallButton.center = newPoint;
[smallButton setTitle:@"缩小" forState:UIControlStateNormal];
[smallButton addTarget:self action:@selector(smallDidPush) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:smallButton];
UIButton *invertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
invertButton.frame = rotateButton.frame;
newPoint = self.view.center;
newPoint.x += 75;
newPoint.y = self.view.frame.size.height - 70;
invertButton.center = newPoint;
[invertButton setTitle:@"反转" forState:UIControlStateNormal];
[invertButton addTarget:self action:@selector(invertDidPush) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:invertButton];
}
- (void)transformWithAnimation {
[UIView beginAnimations:nil context:NULL];
CGAffineTransform transformRotate = CGAffineTransformMakeRotation(_rotate * (M_PI / 180.0));
CGAffineTransform transformScale = CGAffineTransformMakeScale(_scale, _scale);
CGAffineTransform transformAll = CGAffineTransformConcat(transformRotate, transformScale);
if (_needFlip) {
transformAll = CGAffineTransformScale(transformAll, -1.0, 1.0);
}
_imageView.transform = transformAll;
[UIView commitAnimations];
}
- (void)rotateDidPush {
_rotate += 90.0;
if (_rotate >= 360) {
_rotate = 0.0;
}
[self transformWithAnimation];
}
- (void)bigDidPush {
_scale += 0.1;
[self transformWithAnimation];
}
- (void)smallDidPush {
_scale -= 0.1;
[self transformWithAnimation];
}
- (void)invertDidPush {
_needFlip = !_needFlip;
[self transformWithAnimation];
}