旋转按钮特效
2016-08-07 本文已影响424人
GF极客天涯
#import "ViewController.h"
// 设置颜色
define my_color(r,g,b,a) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a]**
// 屏幕的尺寸
define my_size [[UIScreen mainScreen] bounds].size
// 按钮的半径
define my_radius 0.33 * my_size.width
// 设置按钮的旋转速度 值越大旋转的越快
define my_speed 0.25**
@interface ViewController ()
// 定时器
@property(nonatomic,strong)NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 调用加载视图的方法
[self loadSubview];
}
#pragma mark - 加载视图
- (void)loadSubview
{
// 设置背景色
self.view.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
// 设置按钮标题
NSArray *titleArr = @[@"1",@"2",@"3",@"4",@"5",@"6"];
// 设置颜色数组
NSArray *colorArr = @[ my_color(130, 202, 118, 1),
my_color(255, 182, 60, 1),
my_color(135, 66, 255, 1),
my_color(255, 89, 142, 1),
my_color(125, 202, 209, 1),
my_color(100, 178, 255, 1)];
// for循环创建按钮
for (int i = 0; i < titleArr.count; i++) {
CGFloat angle = 30+60*i;
double sinAngle = sin(angle * M_PI / 180);
double cosAngle = cos(angle *M_PI / 180);
NSInteger centerX = my_size.width / 2 +cosAngle *my_radius;
NSInteger centerY = my_size.height / 2 +sinAngle *my_radius;
UIButton *btn = [[UIButton alloc]init];
btn.tag = 120 + i;
btn.bounds = CGRectMake(0, 0, 80, 80);
btn.center = CGPointMake(centerX, centerY);
[btn setTitle:titleArr[i] forState:UIControlStateNormal];
btn.backgroundColor = colorArr[i];
btn.layer.masksToBounds = YES;
btn.layer.cornerRadius = 40.0;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerGo:) userInfo:nil repeats:YES];
}
#pragma mark -按钮被点击了
- (void)btnClick:(UIButton *)sender
{
NSLog(@"这里的按钮被点击了");
}
#pragma mark - 定时器方法
- (void)timerGo:(NSTimer *)timer {
static int count = 0;
++count;
for (int i = 0; i < 6; i++) {
UIButton * button = (UIButton *)[self.view viewWithTag:120 + i];
CGFloat angle = 30 + 60 * i + count * my_speed;
double sinAngle = sin(angle * M_PI / 180);
double cosAngle = cos(angle * M_PI / 180);
CGFloat centerX = my_size.width / 2 + cosAngle * my_radius;
CGFloat centerY = my_size.height / 2 + sinAngle * my_radius;
button.center = CGPointMake(centerX, centerY);
}
if (count == 1000 * 360) {
count = 0;
}
}
- (void)dealloc {
if (_timer) {
[_timer invalidate];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
...
屏幕快照 2016-08-07 11.04.55.png...
123.gif