旋转动画用控件RotateView
最终效果:
源码:
RotateView.h 与 RotateView.m
//
// RotateView.h
// RotateAnimationView
//
// Created by YouXianMing on 14/12/8.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> /**
* 要注意normalInputView与disableInputView的frame值最好与RotateView的bounds值一致
*/
@interface RotateView : UIView /**
* 显示常规的view
*/
@property (nonatomic, strong) UIView *normalInputView;
/**
* 禁用状态的view
*/
@property (nonatomic, strong) UIView *disableInputView;
/**
* 旋转时间
*/
@property (nonatomic, assign) CGFloat rotateDuration;
/**
* view切换时间
*/
@property (nonatomic, assign) CGFloat fadeDuration; /**
* 旋转到向上的位置(默认的位置)
*
* @param animated 是否显示动画
*/
- (void)changeToUpAnimated:(BOOL)animated; /**
* 旋转到向左的位置
*
* @param animated 是否显示动画
*/
- (void)changeToLeftAnimated:(BOOL)animated; /**
* 旋转到向右的位置
*
* @param animated 是否显示动画
*/
- (void)changeToRightAnimated:(BOOL)animated; /**
* 旋转到向下的位置
*
* @param animated 是否显示动画
*/
- (void)changeTodownAnimated:(BOOL)animated; /**
* 渐变到显示常规的view
*
* @param animated 是否显示动画
*/
- (void)fadeToNormalInputViewAnimated:(BOOL)animated; /**
* 渐变到禁用状态的view
*
* @param animated 是否显示动画
*/
- (void)fadeToDisableInputViewAnimated:(BOOL)animated; @end
//
// RotateView.m
// RotateAnimationView
//
// Created by YouXianMing on 14/12/8.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "RotateView.h" static CGFloat defaultDuration = 0.25f; typedef enum : NSUInteger {
UIVIEW_normalInputView = 0xEEFF,
UIVIEW_disableInputView,
} EnumRotateView; @interface RotateView ()
@property (nonatomic, assign) CGAffineTransform defaultTransform;
@end @implementation RotateView - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_defaultTransform = self.transform;
}
return self;
} #pragma mark - 动画的执行
- (void)changeToUpAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:(_rotateDuration > ? _rotateDuration : defaultDuration)
animations:^{
self.transform = _defaultTransform;
}];
} else {
self.transform = _defaultTransform;
} }
- (void)changeToLeftAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:(_rotateDuration > ? _rotateDuration : defaultDuration)
animations:^{
self.transform = CGAffineTransformRotate(_defaultTransform, -M_PI_2);
}];
} else {
self.transform = CGAffineTransformRotate(_defaultTransform, -M_PI_2);
}
}
- (void)changeToRightAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:(_rotateDuration > ? _rotateDuration : defaultDuration)
animations:^{
self.transform = CGAffineTransformRotate(_defaultTransform, M_PI_2);
}];
} else {
self.transform = CGAffineTransformRotate(_defaultTransform, M_PI_2);
}
}
- (void)changeTodownAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:(_rotateDuration > ? _rotateDuration : defaultDuration)
animations:^{
self.transform = CGAffineTransformRotate(_defaultTransform, M_PI);
}];
} else {
self.transform = CGAffineTransformRotate(_defaultTransform, M_PI);
}
}
- (void)fadeToNormalInputViewAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:(_fadeDuration > ? _fadeDuration : defaultDuration)
animations:^{
[self viewWithTag:UIVIEW_normalInputView].alpha = .f;
}];
} else {
[self viewWithTag:UIVIEW_normalInputView].alpha = .f;
}
}
- (void)fadeToDisableInputViewAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:(_fadeDuration > ? _fadeDuration : defaultDuration)
animations:^{
[self viewWithTag:UIVIEW_normalInputView].alpha = .f;
}];
} else {
[self viewWithTag:UIVIEW_normalInputView].alpha = .f;
}
} #pragma mark - 重写setter,getter方法
@synthesize normalInputView = _normalInputView;
- (void)setNormalInputView:(UIView *)normalInputView {
normalInputView.frame = normalInputView.bounds;
normalInputView.userInteractionEnabled = NO;
normalInputView.tag = UIVIEW_normalInputView;
[self addSubview:normalInputView]; [self bringSubviewToFront:normalInputView];
}
- (UIView *)normalInputView {
return [self viewWithTag:UIVIEW_normalInputView];
} @synthesize disableInputView = _disableInputView;
- (void)setDisableInputView:(UIView *)disableInputView {
disableInputView.frame = disableInputView.bounds;
disableInputView.userInteractionEnabled = NO;
disableInputView.tag = UIVIEW_disableInputView;
[self addSubview:disableInputView]; [self sendSubviewToBack:disableInputView];
}
- (UIView *)disableInputView {
return [self viewWithTag:UIVIEW_disableInputView];
} @end
使用的源码:
//
// ViewController.m
// CircleView
//
// Created by YouXianMing on 14/12/9.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "RotateView.h" @interface ViewController () @property (nonatomic, strong) RotateView *rotateView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 输入图片与输出图片
UIImageView *normalView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"normal"]];
UIImageView *disableView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"disable"]]; // 旋转的view
_rotateView = [[RotateView alloc] initWithFrame:normalView.bounds];
_rotateView.center = self.view.center;
_rotateView.normalInputView = normalView;
_rotateView.disableInputView = disableView;
[self.view addSubview:_rotateView]; // 延时
[self performSelector:@selector(excuteEventOne)
withObject:nil
afterDelay:.f]; // 延时
[self performSelector:@selector(excuteEventTwo)
withObject:nil
afterDelay:.f];
}
- (void)excuteEventOne {
[_rotateView changeTodownAnimated:YES];
[_rotateView fadeToDisableInputViewAnimated:YES];
} - (void)excuteEventTwo {
[_rotateView changeToUpAnimated:YES];
[_rotateView fadeToNormalInputViewAnimated:YES];
} @end
较为核心的地方: