UIview 基础 旋转按钮

IRain 发布于 2013/10/14 18:21
阅读 319
收藏 1
iOS

#import "ViewController.h"

#define Left 1

#define You 2

#define Up 3

#define down 4

#define Borange 5


@implementation ViewController


//新建按钮

-(void)test

{

    //新建按钮

    UIButton *btn = [[UIButton alloc]init];

    

    //绑定标签

    btn.tag = Borange;

    

    //设置按钮的位置

    btn.frame = CGRectMake(100, 100, 100, 100);

    

    //设置按钮的颜色

    btn.backgroundColor = [UIColor orangeColor];

    

    //设置按钮的文字 正常状态

    [btn setTitle:@"点一下" forState:UIControlStateNormal];

    

    //设置按钮的文字 高亮状态

    [btn setTitle:@"恭喜发财" forState:UIControlStateHighlighted];

    

    //设置高亮颜色

    [btn setTitleColor:[UIColor redColor]forState:UIControlStateHighlighted];

    

    [self.view addSubview:btn];

}


//新建旋转按钮

-(void)NormalWith:(NSString *)normal hight:(NSString *)hight point:(CGPoint)point tag:(NSInteger    )tag

{

    //新建按钮

    UIButton *btn = [[UIButton alloc]init];

   

    //绑定监听

    btn.tag = tag;

    

    //传入的图片做处理 普通状态

    UIImage *normalImage = [UIImage imageNamed:normal];

    [btn setImage:normalImage forState:UIControlStateNormal];

    

    //传入图片 高亮

    UIImage *hightImage = [UIImage imageNamed:hight];

    [btn setImage:hightImage forState:UIControlStateHighlighted];

    

    //设置位置

    btn.frame = (CGRect){point,normalImage.size};

    

    //绑定监听

    [btn addTarget:self action:@selector(test3:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:btn];

}


//监听按钮

-(void)test3:(UIButton *)Btn

{

    //获取橙色按钮

    UIView *orange = [self.view viewWithTag:Borange];

    

    //获取按钮中心位置

    CGPoint center =  orange.center;

    

    switch (Btn.tag) {

        case Left:

            center.x -= 50;

            break;

        case You:

            center.x += 50;

            break;

        case Up:

            center.y -= 50;

            break;

        case down:

            center.y += 50;

            break;

    }

    

    [UIView beginAnimations:Nil context:nil];

    [UIView setAnimationDuration:0.8];

    //赋值回给中心位置

    orange.center = center;

    [UIView commitAnimations];

    

}


- (void)viewDidLoad

{

    [super viewDidLoad];


    //新建按钮

    [self test];

    

    //新建选择按钮

    //

    [self NormalWith:@"sub_black_prev.png" hight:@"sub_blue_prev.png" point:CGPointMake(25, 350)tag:Left];

    //

    [self NormalWith:@"sub_black_next.png" hight:@"sub_blue_next.png" point:CGPointMake(80, 350)tag:You];

    //

    [self NormalWith:@"sub_black_up.png" hight:@"sub_blue_up.png" point:CGPointMake(55, 315) tag:Up];

    //

    [self NormalWith:@"sub_black_down.png" hight:@"sub_blue_down.png" point:CGPointMake(55, 380)tag:down];

}


- (IBAction)sender:(UIButton *)sender {

    //拿到橙色按钮

    UIView *btn = [self.view viewWithTag:Borange];


    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.8];

    

    switch (sender.tag) {

        case 10:

            btn.transform = CGAffineTransformRotate(btn.transform, -M_PI_4);

            break;

        case 20:

            btn.transform = CGAffineTransformRotate(btn.transform, M_PI_4);

            break;

    }

    [UIView commitAnimations];

}


- (IBAction)jianjia:(UIButton *)sender {

    //获取橙色按钮

    UIView *ben = [self.view viewWithTag:Borange];

    

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.8];

    switch (sender.tag) {

        case 30:

            ben.transform = CGAffineTransformScale(ben.transform, 1.5, 1.5);

            break;

            

        case 40:

            ben.transform = CGAffineTransformScale(ben.transform, 0.5, 0.5);

            break;

    }

    [UIView commitAnimations];

    

}

@end



//新手学习,怎么从左边进去 右边出来,上面上去下面下来呢,求大神指教

加载中
返回顶部
顶部