//布局视图
.m文件
// ControllerView.m
// PhotoAlbum
// Copyright (c) 2014年 Summer. All rights reserved
#import "ControllerView.h"
#import "UIColor+RandomColor.h"
@implementation ControllerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.gestureBtnArr = [NSMutableArray array];
self.animationArr = [NSMutableArray array];
[self titleButton];
[self animationImage];
[self animationButton];
}
return self;
}
- (void)titleButton
{
NSArray *btnArr = @[@"轻拍", @"长按", @"轻扫", @"平移", @"捏合", @"旋转", @"边缘"];
NSInteger n = 0;
for (int i = 0; i < 7; i++) {
UIButton *gestureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
gestureBtn.frame = CGRectMake(20 + 40 * i, 40, 40, 30);
gestureBtn.layer.borderWidth = 1;
gestureBtn.layer.cornerRadius = 5;
gestureBtn.backgroundColor = [UIColor randomColor];
[gestureBtn setTitle:btnArr[n] forState:UIControlStateNormal];
[gestureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
gestureBtn.tag = 100 + i;
[self addSubview:gestureBtn];
[self.gestureBtnArr addObject:gestureBtn];
n++;
}
}
- (void)animationImage
{
self.animationZombieImages = [NSMutableArray array];
for (int i = 0; i < 22; i++) {
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Zombie%d", i + 1] ofType:@"tiff"]];
[_animationZombieImages addObject:image];
}
self.animationSunFlowerImages1 = [NSMutableArray array];
for (int i = 0; i < 18; i++) {
UIImage *image1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"SunFlower_%d", i + 1] ofType:@"tiff"]];
[_animationSunFlowerImages1 addObject:image1];
}
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Meinu1" ofType:@"JPG"]]];
_imageView.userInteractionEnabled = YES;
_imageView.frame = CGRectMake(60, 90, 200, 320);
//设置播放完一组动态图片的时间
_imageView.animationDuration = 1;
[self addSubview:_imageView];
[_imageView release];
}
- (void)animationButton
{
NSArray *animationArray = @[@"开始动画", @"结束动画"];
NSInteger n = 0;
for (int i = 0; i < 2; i++) {
self.animationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_animationBtn.frame = CGRectMake(20 + 150 * i, 430, 130, 30);
_animationBtn.layer.borderWidth = 1;
_animationBtn.layer.cornerRadius = 5;
_animationBtn.backgroundColor = [UIColor randomColor];
[_animationBtn setTitle:animationArray[n] forState:UIControlStateNormal];
[_animationBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_animationBtn.tag = 200 + i;
[self addSubview:_animationBtn];
[self.animationArr addObject:_animationBtn];
n++;
}
}
- (void)dealloc
{
self.imageView = nil;
[super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
.h文件
// ControllerView.h
// PhotoAlbum
// Copyright (c) 2014年 Summer. All rights reserved.
#import <UIKit/UIKit.h>
@interface ControllerView : UIView
@property (nonatomic, retain) NSMutableArray *gestureBtnArr;//存储控制手势切换的按钮
@property (nonatomic, retain) NSMutableArray *animationArr;//存储动画按钮
@property (nonatomic, retain) NSMutableArray *animationZombieImages;
@property (nonatomic, retain) NSMutableArray *animationSunFlowerImages1;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIButton *animationBtn;
@end
//手势操作触发的事件
// RootViewController.m
// PhotoAlbum
// Copyright (c) 2014年 Summer. All rights reserved.
#import "RootViewController.h"
#import "ControllerView.h"
@interface RootViewController ()
{
ControllerView *_controView;
CGFloat _totalRotation;
}
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
_controView = [[ControllerView alloc] init];
_controView.backgroundColor = [UIColor yellowColor];
self.view = _controView;
[_controView release];
//给button添加事件
for (UIButton *btn in _controView.gestureBtnArr) {
[btn addTarget:self action:@selector(changeGesture:) forControlEvents:UIControlEventTouchUpInside];
}
for (UIButton *btn in _controView.animationArr) {
[btn addTarget:self action:@selector(changeAnimation:) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)changeGesture:(UIButton *)btn
{
switch (btn.tag) {
case 100:
[self tapGesture:btn];
break;
case 101:
[self longPressGesture:btn];
break;
case 102:
[self swipeGesture:btn];
break;
case 103:
[self panGesture:btn];
break;
case 104:
[self pinchGesture:btn];
break;
case 105:
[self rotationGesture:btn];
break;
case 106:
[self screenEdgePanGesture:btn];
break;
default:
break;
}
}
#pragma mark tapGesture
- (void)tapGesture:(UIButton *)tapBtn
{
[self recoverImageView:_controView.imageView.gestureRecognizers.firstObject];
[self removeGestureRecognizer];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
//设置轻拍次数
tapGesture.numberOfTapsRequired = 1;
//给视图添加轻拍手势
[_controView.imageView addGestureRecognizer:tapGesture];
[tapGesture release];
}
//轻拍手势
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture
{
static int i = 2;
_controView.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Meinu%d", i] ofType:@"JPG"]];
i++;
if (8 == i) {
i = 1;
}
}
#pragma mark longPressGesture
- (void)longPressGesture:(UIButton *)longPressBtn
{
[self recoverImageView:_controView.imageView.gestureRecognizers.firstObject];
[self removeGestureRecognizer];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
//设置长按触发的最短时间
longPressGesture.minimumPressDuration = 1;
[_controView.imageView addGestureRecognizer:longPressGesture];
[longPressGesture release];
}
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture
{
UIImageWriteToSavedPhotosAlbum(_controView.imageView.image, nil, nil, nil);
if (UIGestureRecognizerStateBegan == longPressGesture.state) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否保存到相册" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
[alertView show];
[alertView release];
}
}
#pragma mark swipeGesture
- (void)swipeGesture:(UIButton *)swipeBtn
{
[self recoverImageView:_controView.imageView.gestureRecognizers.firstObject];
[self removeGestureRecognizer];
_controView.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NBA1" ofType:@"JPG"]];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[_controView.imageView addGestureRecognizer:swipeGesture];
[swipeGesture release];
}
- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture
{
static int i = 1;
if (swipeGesture.direction == 2) {
i++;
if (7 == i) {
i = 1;
}
_controView.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"NBA%d", i] ofType:@"JPG"]];
}else if (swipeGesture.direction == 3){
i--;
if (0 == i) {
i = 5;
}
_controView.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"NBA%d", i] ofType:@"JPG"]];
}
}
#pragma mark panGesture
- (void)panGesture:(UIButton *)panBtn
{
[self recoverImageView:_controView.imageView.gestureRecognizers.firstObject];
[self removeGestureRecognizer];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[_controView.imageView addGestureRecognizer:panGesture];
[panGesture release];
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
{
//获取平移前后的改变量
CGPoint point = [panGesture translationInView:panGesture.view];
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
//将之前的增量清零
[panGesture setTranslation:CGPointZero inView:panGesture.view];
}
#pragma mark pinchGesture
- (void)pinchGesture:(UIButton *)pinchBtn
{
[self recoverImageView:_controView.imageView.gestureRecognizers.firstObject];
[self removeGestureRecognizer];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
[_controView.imageView addGestureRecognizer:pinchGesture];
[pinchGesture release];
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture
{
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
pinchGesture.scale = 1.0;
}
#pragma mark rotationGesture
- (void)rotationGesture:(UIButton *)rotationBtn
{
[self recoverImageView:_controView.imageView.gestureRecognizers.firstObject];
[self removeGestureRecognizer];
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];
[_controView.imageView addGestureRecognizer:rotationGesture];
[rotationGesture release];
}
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture
{
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
_totalRotation += rotationGesture.rotation;
rotationGesture.rotation = 0;
}
#pragma mark screenEdgePanGesture
- (void)screenEdgePanGesture:(UIButton *)screenBtn
{
[self recoverImageView:_controView.imageView.gestureRecognizers.firstObject];
[self removeGestureRecognizer];
UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgePanGesture:)];
screenEdgePanGesture.edges = UIRectEdgeLeft;
[self.view addGestureRecognizer:screenEdgePanGesture];
[screenEdgePanGesture release];
}
- (void)handleScreenEdgePanGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgePanGesture
{
_controView.imageView.animationImages = _controView.animationSunFlowerImages1;
[_controView.imageView startAnimating];
}
//还原视图
- (void)recoverImageView:(UIGestureRecognizer *)gesture
{
if ([gesture respondsToSelector:@selector(setTranslation:inView:)]) {
_controView.imageView.frame = CGRectMake(60, 90, 200, 320);
}else if ([gesture respondsToSelector:@selector(setRotation:)]){
UIRotationGestureRecognizer *tempGesture = (UIRotationGestureRecognizer *)gesture;
tempGesture.view.transform = CGAffineTransformRotate(tempGesture.view.transform, -_totalRotation);
_totalRotation = 0;
}else if ([gesture respondsToSelector:@selector(setScale:)]){
_controView.imageView.frame = CGRectMake(60, 90, 200, 320);
}
self.view.frame = [UIScreen mainScreen].bounds;
}
//移除手势
- (void)removeGestureRecognizer
{
for (UIGestureRecognizer *gesture in _controView.imageView.gestureRecognizers) {
[_controView.imageView removeGestureRecognizer:gesture];
}
}
- (void)changeAnimation:(UIButton *)btn
{
switch (btn.tag) {
case 200:
[self startAnimating:btn];
break;
case 201:
[self stopAnimating:btn];
break;
default:
break;
}
}
//开始播放动态图片.开始跑
- (void)startAnimating:(UIButton *)startBtn
{
_controView.imageView.animationImages = _controView.animationZombieImages;
[_controView.imageView startAnimating];
}
//结束动画
- (void)stopAnimating:(UIButton *)stopBtn
{
[_controView.imageView stopAnimating];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (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