旋转动画用控件RotateView

旋转动画用控件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 > 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

较为核心的地方:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值