【IOS】按钮双图片翻转动画

此代码实现了一个自定义的CMSCoinView,它包含一个主视图和一个次视图,能够进行翻转动画,用于展示关怀版切换按钮。用户可以设置主视图和次视图,并通过点击事件监听正面(显示主视图)还是反面(显示次视图)。点击后,视图会以0.5秒的动画时间进行翻转效果。

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

CMSCoinView.h

#import <UIKit/UIKit.h>


typedef void (^ClickBlock)(BOOL isObcerse);

@interface CMSCoinView : UIView
 
-(id)initWithPrimaryView:(UIView *)view1 andSecondaryView:(UIView *)view2 inFrame:(CGRect)frame;
 
@property (nonatomic,strong) UIView *primaryView;
@property (nonatomic,strong) UIView *secondaryView;
@property (nonatomic,assign)float spinTime;

@property (nonatomic,strong)ClickBlock block;
 
@end


CMSCoinView.m

#import "CMSCoinView.h"

@implementation CMSCoinView{
    bool _displayingPrimary;
    float _spinTime;
    
    BOOL _isObcerse;//是否正面
    
    ClickBlock _clickBlock;
}
 
-(id)initWithPrimaryView:(UIView *)primaryView andSecondaryView: (UIView *)secondaryView inFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.primaryView = primaryView;
        self.secondaryView = secondaryView;
        _displayingPrimary = YES;
        _spinTime = 0.5;
        _isObcerse = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flipTouched)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

-(void)setPrimaryView:(UIView *)primaryView{
    _primaryView = primaryView;
    CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.primaryView.frame = frame;
    self.primaryView.backgroundColor = UIColor.clearColor;
    self.primaryView.userInteractionEnabled = YES;
    [self addSubview:self.primaryView];
}
 
-(void)setSecondaryView:(UIView *)secondaryView{
    _secondaryView = secondaryView;
    CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.secondaryView.frame = frame;
    self.secondaryView.backgroundColor = UIColor.clearColor;
    self.secondaryView.userInteractionEnabled = YES;
    [self addSubview: self.secondaryView];
    [self sendSubviewToBack:self.secondaryView];
    self.secondaryView.hidden = YES;
}

-(void)flipTouched{
    _isObcerse = !_isObcerse;
    if (_isObcerse) {
        [self showFront];
    }else{
        [self showBack];
    }
}
 
-(void)showBack{
    self.secondaryView.hidden = NO;
    [UIView transitionFromView:self.primaryView toView:self.secondaryView
                      duration:_spinTime
                       options: UIViewAnimationOptionTransitionFlipFromRight+UIViewAnimationOptionCurveEaseInOut
                    completion:^(BOOL finished) {
        if (finished) {
            self->_displayingPrimary = !self->_displayingPrimary;
            if(self->_clickBlock){
                self->_clickBlock(self->_isObcerse);
            }
        }
    }];
}
-(void)showFront{
    [UIView transitionFromView:self.secondaryView toView:self.primaryView
                      duration:_spinTime
                       options: UIViewAnimationOptionTransitionFlipFromRight+UIViewAnimationOptionCurveEaseInOut
                    completion:^(BOOL finished) {
        if (finished) {
            self->_displayingPrimary = !self->_displayingPrimary;
            if(self->_clickBlock){
                self->_clickBlock(self->_isObcerse);
            }
        }
    }];
}


-(void)setBlock:(ClickBlock)block{
    _clickBlock = block;
}

@end

使用:

//关怀版切换按钮
    _agedButton = [[CMSCoinView alloc]initWithPrimaryView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"aged"]] andSecondaryView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"young"]] inFrame:CGRectMake(15, (CGRectGetHeight(_titleLabel.frame)-20)/2.0, 20, 20)];
    [_titleLabel addSubview:_agedButton];
    _agedButton.block = ^(BOOL isObcerse) {
        NSLog(@"isObcerse = %d",isObcerse);
    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值