硬币翻转动画效果

这篇博客展示了如何实现一个逼真的硬币翻转动画效果,通过示例代码和最终的动画展示,详细介绍了实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

  

  


示例

UIImageView *firstImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
UIImageView *secondImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"second.jpg"]];
    
CMSCoinView *coinView = [[CMSCoinView alloc] initWithFrame:CGRectMake(50.0, 50.0, 120.0, 120.0) view:self.view primaryView:firstImageView secondaryView:secondImageView];
coinView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];


实现类文件

// .h文件
#import <UIKit/UIKit.h>

@interface CMSCoinView : UIView

- (id)initWithFrame:(CGRect)frame view:(UIView *)superview primaryView:(UIView *)primaryView secondaryView:(UIView *)secondaryView;

@property (nonatomic, strong) UIView *primaryView;
@property (nonatomic, strong) UIView *secondaryView;
@property (nonatomic, assign) float spinTime;

@end
// .m文件
#import "CMSCoinView.h"
#import <QuartzCore/QuartzCore.h>

@interface CMSCoinView ()
{
    bool displayingPrimary;
}

@end

@implementation CMSCoinView

// 初始化
- (id)initWithFrame:(CGRect)frame view:(UIView *)superview primaryView:(UIView *)primaryView secondaryView:(UIView *)secondaryView
{
    self = [super init];
    if (self)
    {
        self.frame = frame;
        
        if (superview)
        {
            [superview addSubview:self];
        }
        
        // Initialization code
        self.primaryView = primaryView;
        self.secondaryView = secondaryView;
        
        displayingPrimary = YES;
        self.spinTime = 1.0;
    }
    return self;
}

#pragma mark - set方法

- (void)setPrimaryView:(UIView *)primaryView
{
    _primaryView = primaryView;
    
    CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.primaryView.frame = frame;
    [self roundView: self.primaryView];
    self.primaryView.userInteractionEnabled = YES;
    [self addSubview: self.primaryView];
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flipTouched:)];
    gesture.numberOfTapsRequired = 1;
    [self.primaryView addGestureRecognizer:gesture];
    [self roundView:self];
}

- (void)setSecondaryView:(UIView *)secondaryView
{
    _secondaryView = secondaryView;
    CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.secondaryView.frame = frame;
    [self roundView: self.secondaryView];
    self.secondaryView.userInteractionEnabled = YES;
    [self addSubview: self.secondaryView];
    [self sendSubviewToBack:self.secondaryView];
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flipTouched:)];
    gesture.numberOfTapsRequired = 1;
    [self.secondaryView addGestureRecognizer:gesture];
    [self roundView:self];
}

- (void)setSpinTime:(float)spinTime
{
    _spinTime = spinTime;
}

#pragma mark - 视图属性设置

- (void)roundView:(UIView *)view
{
    view.layer.cornerRadius = (self.frame.size.height / 2);
    view.layer.masksToBounds = YES;
}

#pragma mark - 响应方法
- (void)flipTouched:(UITapGestureRecognizer *)sender
{
    UIView *fromView = (displayingPrimary ? self.primaryView : self.secondaryView);
    UIView *toView = (displayingPrimary ? self.secondaryView : self.primaryView);
    UIViewAnimationOptions options = (displayingPrimary ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight) + UIViewAnimationOptionCurveEaseInOut;
    
    [UIView transitionFromView:fromView
                        toView:toView
                      duration:self.spinTime
                       options:options
                    completion:^(BOOL finished) {
                        if (finished)
                        {
                            displayingPrimary = !displayingPrimary;
                        }
                    }
     ];
}

@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值