码迷,mamicode.com
首页 > 其他好文 > 详细

旋转动画用控件RotateView

时间:2014-12-10 00:20:00      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   使用   sp   for   

旋转动画用控件RotateView

bubuko.com,布布扣

最终效果:

bubuko.com,布布扣

源码:

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 > 0 ? _rotateDuration : defaultDuration)
                         animations:^{
                             self.transform = _defaultTransform;
                         }];
    } else {
        self.transform = _defaultTransform;
    }

}
- (void)changeToLeftAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:(_rotateDuration > 0 ? _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 > 0 ? _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 > 0 ? _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 > 0 ? _fadeDuration : defaultDuration)
                         animations:^{
                             [self viewWithTag:UIVIEW_normalInputView].alpha = 1.f;
                         }];
    } else {
        [self viewWithTag:UIVIEW_normalInputView].alpha = 1.f;
    }
}
- (void)fadeToDisableInputViewAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:(_fadeDuration > 0 ? _fadeDuration : defaultDuration)
                         animations:^{
                             [self viewWithTag:UIVIEW_normalInputView].alpha = 0.f;
                         }];
    } else {
        [self viewWithTag:UIVIEW_normalInputView].alpha = 0.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:7.f];
    
    // 延时
    [self performSelector:@selector(excuteEventTwo)
               withObject:nil
               afterDelay:9.f];
}
- (void)excuteEventOne {
    [_rotateView changeTodownAnimated:YES];
    [_rotateView fadeToDisableInputViewAnimated:YES];
}

- (void)excuteEventTwo {
    [_rotateView changeToUpAnimated:YES];
    [_rotateView fadeToNormalInputViewAnimated:YES];
}

@end

较为核心的地方:

bubuko.com,布布扣

 

旋转动画用控件RotateView

标签:style   blog   http   io   ar   color   使用   sp   for   

原文地址:http://www.cnblogs.com/YouXianMing/p/4152506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!