UIView+Animation.h
#import
float radiansForDegrees(int degrees);
@interface UIView (Animation)
// 位移
- (void)moveTo:(CGPoint)destination duration:(float)secs option:(UIViewAnimationOptions)option;
- (void)moveTo:(CGPoint)destination duration:(float)secs option:(UIViewAnimationOptions)option delegate:(id)delegate callback:(SEL)method;
- (void)raceTo:(CGPoint)destination withSnapBack:(BOOL)withSnapBack;
- (void)raceTo:(CGPoint)destination withSnapBack:(BOOL)withSnapBack delegate:(id)delegate callback:(SEL)method;
// 形变
/**
* 旋转
*/
- (void)rotate:(int)degrees secs:(float)secs delegate:(id)delegate callback:(SEL)method;
/**
* 缩放
*/
- (void)scale:(float)secs x:(float)scaleX y:(float)scaleY delegate:(id)delegate callback:(SEL)method;
/**
* 顺时针旋转
*
* @param secs 动画执行时间
*/
- (void)spinClockwise:(float)secs;
/**
* 逆时针旋转
*
* @param secs 动画执行时间
*/
- (void)spinCounterClockwise:(float)secs;
/**
* 反翻页效果
*
* @param secs 动画执行时间
*/
- (void)curlDown:(float)secs;
/**
* 视图翻页后消失
*
* @param secs 动画执行时间
*/
- (void)curlUpAndAway:(float)secs;
/**
* 旋转缩放到一点后消失
*
* @param secs 动画执行时间
*/
- (void)drainAway:(float)secs;
/**
* 将视图改变到一定透明度
*
* @param newAlpha alpha
* @param secs 动画执行时间
*/
- (void)changeAlpha:(float)newAlpha secs:(float)secs;
/**
* 改变透明度结束动画后还原
*
* @param secs alpha
* @param continuously 是否循环
*/
- (void)pulse:(float)secs continuously:(BOOL)continuously;
/**
* 以渐变方式添加子控件
*
* @param subview 需要添加的子控件
*/
- (void)addSubviewWithFadeAnimation:(UIView *)subview;
@end
UIView+Animation.m
#import "UIView+Animation.h"
// Very helpful function
float radiansForDegrees(int degrees) {
return degrees * M_PI / 180;
}
@implementation UIView (Animation)
#pragma mark - Moves
- (void)moveTo:(CGPoint)destination duration:(float)secs option:(UIViewAnimationOptions)option {
[self moveTo:destination duration:secs option:option delegate:nil callback:nil];
}
- (void)moveTo:(CGPoint)destination duration:(float)secs option:(UIViewAnimationOptions)option delegate:(id)delegate callback:(SEL)method {
[UIView animateWithDuration:secs delay:0.0 options:option
animations:^{
self.frame = CGRectMake(destination.x,destination.y, self.frame.size.width, self.frame.size.height);
}
completion:^(BOOL finished) {
if (delegate != nil) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[delegate performSelector:method];
#pragma clang diagnostic pop
}
}];
}
- (void)raceTo:(CGPoint)destination withSnapBack:(BOOL)withSnapBack {
[self raceTo:destination withSnapBack:withSnapBack delegate:nil callback:nil];
}
- (void)raceTo:(CGPoint)destination withSnapBack:(BOOL)withSnapBack delegate:(id)delegate callback:(SEL)method {
CGPoint stopPoint = destination;
if (withSnapBack) {
// Determine our stop point, from which we will "snap back" to the final destination
int diffx = destination.x - self.frame.origin.x;
int diffy = destination.y - self.frame.origin.y;
if (diffx < 0) {
// Destination is to the left of current position
stopPoint.x -= 10.0;
} else if (diffx > 0) {
stopPoint.x += 10.0;
}
if (diffy < 0) {
// Destination is to the left of current position
stopPoint.y -= 10.0;
} else if (diffy > 0) {
stopPoint.y += 10.0;
}
}
// Do the animation
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.frame = CGRectMake(stopPoint.x, stopPoint.y, self.frame.size.width, self.frame.size.height);
}
completion:^(BOOL finished) {
if (withSnapBack) {
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
self.frame = CGRectMake(destination.x, destination.y, self.frame.size.width, self.frame.size.height);
}
completion:^(BOOL finished) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[delegate performSelector:method];
#pragma clang diagnostic pop
}];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[delegate performSelector:method];
#pragma clang diagnostic pop
}
}];
}
#pragma mark - Transforms
- (void)rotate:(int)degrees secs:(float)secs delegate:(id)delegate callback:(SEL)method {
[UIView animateWithDuration:secs
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
self.transform = CGAffineTransformRotate(self.transform, radiansForDegrees(degrees));
}
completion:^(BOOL finished) {
if (delegate != nil) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[delegate performSelector:method];
#pragma clang diagnostic pop
}
}];
}
- (void)scale:(float)secs x:(float)scaleX y:(float)scaleY delegate:(id)delegate callback:(SEL)method {
[UIView animateWithDuration:secs
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
self.transform = CGAffineTransformScale(self.transform, scaleX, scaleY);
}
completion:^(BOOL finished) {
if (delegate != nil) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[delegate performSelector:method];
#pragma clang diagnostic pop
}
}];
}
- (void)spinClockwise:(float)secs {
[UIView animateWithDuration:secs/4
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
self.transform = CGAffineTransformRotate(self.transform, radiansForDegrees(90));
}
completion:^(BOOL finished) {
[self spinClockwise:secs];
}];
}
- (void)spinCounterClockwise:(float)secs {
[UIView animateWithDuration:secs/4
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
self.transform = CGAffineTransformRotate(self.transform, radiansForDegrees(270));
}
completion:^(BOOL finished) {
[self spinCounterClockwise:secs];
}];
}
#pragma mark - Transitions
- (void)curlDown:(float)secs {
[UIView transitionWithView:self duration:secs
options:UIViewAnimationOptionTransitionCurlDown
animations:^ { [self setAlpha:1.0]; }
completion:nil];
}
- (void)curlUpAndAway:(float)secs {
[UIView transitionWithView:self duration:secs
options:UIViewAnimationOptionTransitionCurlUp
animations:^ { [self setAlpha:0]; }
completion:nil];
}
- (void)drainAway:(float)secs {
self.tag = 20;
[NSTimer scheduledTimerWithTimeInterval:secs/50 target:self selector:@selector(drainTimer:) userInfo:nil repeats:YES];
}
- (void)drainTimer:(NSTimer*)timer {
CGAffineTransform trans = CGAffineTransformRotate(CGAffineTransformScale(self.transform, 0.9, 0.9),0.314);
self.transform = trans;
self.alpha = self.alpha * 0.98;
self.tag = self.tag - 1;
if (self.tag <= 0) {
[timer invalidate];
[self removeFromSuperview];
}
}
#pragma mark - Effects
- (void)changeAlpha:(float)newAlpha secs:(float)secs {
[UIView animateWithDuration:secs
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
self.alpha = newAlpha;
}
completion:nil];
}
- (void)pulse:(float)secs continuously:(BOOL)continuously {
[UIView animateWithDuration:secs/2
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Fade out, but not completely
self.alpha = 0.3;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:secs/2
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Fade in
self.alpha = 1.0;
}
completion:^(BOOL finished) {
if (continuously) {
[self pulse:secs continuously:continuously];
}
}];
}];
}
#pragma mark - add subview
- (void)addSubviewWithFadeAnimation:(UIView *)subview {
CGFloat finalAlpha = subview.alpha;
subview.alpha = 0.0;
[self addSubview:subview];
[UIView animateWithDuration:0.2 animations:^{
subview.alpha = finalAlpha;
}];
}
@end
ps:看到这些基本效果还是比较不错的自己留着,我是搬运工!!!