UIView分装成按钮

按钮实现动画效果,用集成UIVIew的类来实现;放好需要变化的控件;效果图如下;按下去红色背景慢慢出来;




分装的button

//

//  CJWButton.h

//  动画按钮

//

//  Created by CJW on 17/1/5.

//  Copyright © 2017 cjw. All rights reserved.

//


#import <UIKit/UIKit.h>

@class CJWButton;

@protocol CJWButtonDelegate <NSObject>


-(void)finshByEnventOnCJWButton:(CJWButton*)button;


@end



@interface CJWButton : UIView


@property (nonatomic,weak) id <CJWButtonDelegate>  delegate;

@property (nonatomic,strong) UIFont * font;

@property (nonatomic,strong) NSString * text;

@property (nonatomic,strong) UIColor * normalTextColor;

@property (nonatomic,strong) UIColor * heghtTextColor;

@property (nonatomic,strong) UIColor * animationColor;


@property (nonatomic) CGFloat animationWith;

@property (nonatomic) CGFloat toDurationTime;

@property (nonatomic) CGFloat toNormalTime;


@end




//

//  CJWButton.m

//  动画按钮

//

//  Created by CJW on 17/1/5.

//  Copyright © 2017 cjw. All rights reserved.

//


#import "CJWButton.h"

#define SHOW_WIEW_WIDTH   1000

#define TIME_END_DURATION 0.5

#define TIME_NOR_DURANTION 0.35


@interface CJWButton()


@property (nonatomic,strong) UIButton * button;

@property (nonatomic,strong) UILabel  * normalLable;

@property (nonatomic,strong) UILabel  * heightLable;

@property (nonatomic,strong) UIView   * showView;


@end



@implementation CJWButton


-(instancetype)initWithFrame:(CGRect)frame{

   self = [super initWithFrame:frame];

    if (self){


        self.layer.masksToBounds = YES;

        [self creatAnimationView];

        [self creatAnimationButton:frame];

        [self creatAnimationLable:frame];

    }

    return self;

}

//创建动画View

-(void)creatAnimationView{

    self.showView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SHOW_WIEW_WIDTH, 0)];

    self.showView.backgroundColor = [UIColor redColor];

    self.showView.userInteractionEnabled = NO;

    self.showView.center   =   CGPointMake(self.frame.size.width/2.f, self.frame.size.height/2.f);

    self.showView.transform = CGAffineTransformMakeRotation(45 * M_PI / 180);

    self.showView.alpha = 0.f;

    [self addSubview:self.showView];


}

//创建按钮

-(void)creatAnimationButton:(CGRect)frame{

    self.button = [[UIButton alloc]initWithFrame:frame];

    [self addSubview:self.button];

    //按钮点击;

    {

         [self.button addTarget:self action:@selector(CJWBUttonTouchDown) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];

         [self.button addTarget:self action:@selector(CJWButtonTouchInside) forControlEvents: UIControlEventTouchUpInside];

    }

 

}

//按钮按下去

-(void)CJWBUttonTouchDown{

    self.showView.bounds = ((CALayer *)(self.showView.layer.presentationLayer)).bounds;

    self.showView.alpha = ((CALayer*)(self.showView.layer.presentationLayer)).opacity;

    self.normalLable.alpha    = ((CALayer *)self.normalLable.layer.presentationLayer).opacity;

    self.heightLable.alpha = ((CALayer *)self.heightLable.layer.presentationLayer).opacity;

    [self.showView.layer removeAllAnimations];

   [UIView animateWithDuration:self.toDurationTime <= 0 ? TIME_END_DURATION:self.toDurationTime  animations:^{

        self.showView.bounds = CGRectMake(0, 0, SHOW_WIEW_WIDTH ,(self.animationWith<=0 ? SHOW_WIEW_WIDTH:self.animationWith) );

       self.showView.alpha = 1;

       

       self.normalLable.alpha = 0.f;

       self.heightLable.alpha = 1.f;

       

    } completion:^(BOOL finished) {

        //按完了,就让代理执行方法;

        if (finished){

            if (self.delegate && ([self.delegate respondsToSelector:@selector(finshByEnventOnCJWButton:)])){

                [self.delegate finshByEnventOnCJWButton:self];

            }

        }

    }];

}

//按钮离开

-(void)CJWButtonTouchInside{

    [self.showView.layer removeAllAnimations];

    [UIView animateWithDuration:self.toNormalTime<=0 ? TIME_NOR_DURANTION:self.toNormalTime animations:^{

        self.showView.bounds = CGRectMake(0, 0,SHOW_WIEW_WIDTH, 0);

        self.showView.alpha = 0.f;

        self.normalLable.alpha = 1.f;

        self.heightLable.alpha = 0.f;

    }];

}

//创建2label显示不同时刻的文字;

-(void)creatAnimationLable:(CGRect)frame{

    self.normalLable = [[UILabel alloc]initWithFrame:frame];

    self.heightLable = [[UILabel alloc]initWithFrame:frame];

    

    self.normalLable.textAlignment = NSTextAlignmentCenter;

    self.heightLable.textAlignment = NSTextAlignmentCenter;

    

    self.normalLable.alpha = 1.f;

    self.heightLable.alpha = 0.f;

    

    [self addSubview:self.heightLable];

    [self addSubview:self.normalLable];

}


@synthesize font = _font;

-(void)setFont:(UIFont *)font {

    _font = font;

    self.normalLable.font = font;

    self.heightLable.font = font;

}

-(UIFont *)font {

    return _font;

}

@synthesize text = _text;

-(void)setText:(NSString *)text {

    _text = text;

    self.normalLable.text = text;

    self.heightLable.text = text;

}

-(NSString *)text {

    return  _text;

}

@synthesize normalTextColor = _normalTextColor;

-(void)setNormalTextColor:(UIColor *)normalTextColor {

    _normalTextColor = normalTextColor;

    self.normalLable.textColor = normalTextColor;

}

-(UIColor *)normalTextColor {

    return _normalTextColor;

}

@synthesize heghtTextColor = _heghtTextColor;

-(void)setHeghtTextColor:(UIColor *)heghtTextColor {

    _heghtTextColor = heghtTextColor;

    self.heightLable.textColor = heghtTextColor;

}

-(UIColor *)heghtTextColor {

    return _heghtTextColor;

}

@synthesize animationColor = _animationColor;

-(void)setAnimationColor:(UIColor *)animationColor {

    _animationColor = animationColor;

    self.showView.backgroundColor = animationColor;

}

-(UIColor *)animationColor {

    return _animationColor;

}


@end




使用;

//

//  ViewController.m

//  动画按钮

//

//  Created by CJW on 17/1/5.

//  Copyright © 2017 cjw. All rights reserved.

//


#import "ViewController.h"

#import "CJWButton.h"

@interface ViewController () <CJWButtonDelegate>


@property (nonatomic,strong) CJWButton * button;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.button = [[CJWButton alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];

    self.button.center = CGPointMake(self.view.bounds.size.width/2.f, self.view.bounds.size.height/2.f);

    self.button.text = @"金屋藏娇";

    self.button.animationWith = 150;

    self.button.layer.borderWidth = 1.f;

    self.button.delegate = self;

    self.button.normalTextColor = [UIColor blackColor];

    self.button.heghtTextColor = [UIColor blueColor];

    self.button.animationColor = [UIColor redColor];

    [self.view addSubview: self.button];

    

}

-(void)finshByEnventOnCJWButton:(CJWButton *)button{

    NSLog(@"%@",@"藏娇的是你");

}



@end































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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值