iOS 简单的按钮弹出效果

#import "ViewController.h"

@interface ViewController ()
{
    UIButton *btn1;
    UIButton *btn2;
    UIButton *btn3;

    float angle;
    float imageviewAngle;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    btn1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-60, self.view.frame.size.height-60, 40, 40)];
    [btn1 setBackgroundColor:[UIColor redColor]];
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    btn2 = [[UIButton alloc] initWithFrame:btn1.frame];
    [btn2 setBackgroundColor:[UIColor redColor]];

    btn3 = [[UIButton alloc] initWithFrame:btn1.frame];
    [btn3 setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    [self.view addSubview:btn1];

    [btn1 bringSubviewToFront:self.view];


}

- (void)btnClick:(id)sender
{
    switch ([sender tag]) {
        case 0:
        {

            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.1f];

            CGRect frame1 = btn2.frame;
            frame1.origin.x -= 100;
            [btn2 setFrame:frame1];

            CGRect frame2 = btn3.frame;
            frame2.origin.y -= 100;
            [btn3 setFrame:frame2];

            [UIView commitAnimations];

            [sender setTag:1];

        } break;

        case 1:
        {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.1f];

            CGRect frame1 = btn2.frame;
            frame1.origin.x += 100;
            [btn2 setFrame:frame1];

            CGRect frame2 = btn3.frame;
            frame2.origin.y += 100;
            [btn3 setFrame:frame2];

            [UIView commitAnimations];

            [sender setTag:0];

        } break;

        default:
            break;
    }
}


@end

你可能感兴趣的:(ios,UIButton)