xcode 不值钱的动画UIButton

 

#import "ViewController.h"

@interface ViewController ()
/**
 按钮
 */
@property(nonatomic,weak)UIButton * btn;
/**
 遮盖按钮
 */
@property(nonatomic,weak)UIButton * alpahaBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景图片以便区分
    UIImageView * imgType=[[UIImageView alloc]initWithFrame:self.view.frame];
    imgType.image=[UIImage imageNamed:@"timg1"];
    [self.view addSubview:imgType];
   
    //创建阴影按钮
    UIButton * alpahaBtnType=[[UIButton alloc]init];
    //设置全屏
    alpahaBtnType.frame=self.view.frame;
    //设置背景颜色
    alpahaBtnType.backgroundColor=[UIColor lightGrayColor];
    //设置透明度
    alpahaBtnType.alpha=0.0;
    self.alpahaBtn=alpahaBtnType;
    [self.view addSubview:alpahaBtnType];
    [self.view bringSubviewToFront:alpahaBtnType];
    
    
    CGFloat cgW=self.view.bounds.size.width;
    CGFloat imgW=174;
    CGFloat imgH=272;
    CGFloat cgY=(cgW-imgW)*0.5;
    //创建按钮
    UIButton * btnType=[[UIButton alloc]init];
    //设置图片
    [btnType setImage:[UIImage imageNamed:@"timg"] forState:UIControlStateNormal];
    //设置位置
    btnType.frame=CGRectMake(cgY, cgY, imgW, imgH);
    self.btn=btnType;
    [self.view addSubview:btnType];
    
    /**
     设置点击事件
     */
    [alpahaBtnType addTarget:self action:@selector(alpahaBtnTypeClick) forControlEvents:UIControlEventTouchUpInside];
    [btnType addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
/**
 消失阴影
 还原图片
 */
-(void)alpahaBtnTypeClick{
   //调用动画
    [UIView animateWithDuration:2.0 animations:^{
        CGFloat cgW=self.view.bounds.size.width;
        CGFloat imgW=174;
        CGFloat imgH=272;
        CGFloat cgY=(cgW-imgW)*0.5;
      self.btn.frame=CGRectMake(cgY, cgY, imgW, imgH);
        self.alpahaBtn.alpha=0.0;
    }];
}
/**
 点击发大图片
 点击还原图片
 */
-(void)btnClick{
    if (self.alpahaBtn.alpha==0) {
        //调用动画
        [UIView animateWithDuration:1.5 animations:^{
//            NSLog(@"%@",NSStringFromCGRect(self.btn.frame));
            CGFloat cgX=self.btn.frame.origin.x;
            CGFloat cgY=self.btn.frame.origin.y;
            CGFloat cgW=self.btn.frame.size.width;
            CGFloat cgH=self.btn.frame.size.height;
            self.btn.frame=CGRectMake(cgX-(cgX*0.5), cgY-(cgY*0.5), cgW+(cgW*0.5), cgH+(cgH*0.5));
            self.alpahaBtn.alpha=1;
            
        }];
    }else{
        [self alpahaBtnTypeClick];
    }
}
@end

功能很简单就是点击按钮加大图片 在点击图片还原回去

判断条件主要判断阴影是否为0 判断阴影按钮的透明度

 

posted @ 2016-06-05 21:33  与格律上  阅读(219)  评论(0编辑  收藏  举报