#import <UIKit/UIKit.h>
@interface WheelView : UIView
+ (instancetype)wheelView;
- (void)startAnimation;
- (void)stopAnimation;
@end
#import "WheelView.h"
#import "WButton.h"
@interface WheelView ()
@property (weak, nonatomic) IBOutlet UIImageView *centerView;
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) CADisplayLink *link;
- (IBAction)chooseNumber:(UIButton *)sender;
@end
@implementation WheelView
+ (instancetype)wheelView{
return [[[NSBundle mainBundle] loadNibNamed:@"WheelView" owner:nil options:nil] lastObject];
}
//添加按钮
- (void)awakeFromNib{
self.centerView.userInteractionEnabled = YES;
//大图片
UIImage *img = [UIImage imageNamed:@"LuckyAstrology"];
UIImage *imgSelected = [UIImage imageNamed:@"LuckyAstrologyPressed"];
//从大图片中裁剪对应星座的图片
CGFloat smallW = img.size.width / 12 * [UIScreen mainScreen].scale;
CGFloat smallH = img.size.height * [UIScreen mainScreen].scale;
// 添加12个按钮
for (int index = 0; index < 12; index++) {
WButton *btn = [WButton buttonWithType:UIButtonTypeCustom];
CGRect smallRect = CGRectMake(index * smallW, 0, smallW, smallH);
// CGImageCreateWithImageInRect只认像素
CGImageRef smallImage = CGImageCreateWithImageInRect(img.CGImage, smallRect);
[btn setImage:[UIImage imageWithCGImage:smallImage] forState:UIControlStateNormal];
CGImageRef smallImageSelected = CGImageCreateWithImageInRect(imgSelected.CGImage, smallRect);
[btn setImage:[UIImage imageWithCGImage:smallImageSelected] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
btn.bounds = CGRectMake(0, 0, 68, 143);
// 设置锚点和位置
btn.layer.anchorPoint = CGPointMake(0.5, 1);
btn.layer.position = CGPointMake(self.centerView.frame.size.width * 0.5, self.centerView.frame.size.height * 0.5);
// 设置旋转角度(绕着锚点进行旋转)
CGFloat angle = (30 * index) / 180.0 * M_PI;
btn.transform = CGAffineTransformMakeRotation(angle);
btn.layer.masksToBounds = YES;
// 监听按钮点击
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
[self.centerView addSubview:btn];
btn.tag = index;
//
if (index == 0) {
[self btnClick:btn];
}
}
}
#pragma mark - btnAction
- (void)btnClick:(WButton *)btn{
self.selectedBtn.selected = NO;
btn.selected = YES;
self.selectedBtn = btn;
}
//核心动画不会改图层的属性,旋转式假象
//开始不停旋转
- (void)startAnimation{
if (self.link) {
return;
}
//1秒刷新60次
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
self.link = link;
}
- (void)update{
self.centerView.transform = CGAffineTransformRotate(self.centerView.transform, M_PI / 100);
}
- (void)stopAnimation{
[self.link invalidate];
self.link = nil;
}
- (IBAction)chooseNumber:(UIButton *)sender {
[self stopAnimation];
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform.rotation";
anim.toValue = @(2 * M_PI * 3);
anim.duration = 2;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.delegate = self;
[self.centerView.layer addAnimation:anim forKey:nil];
self.userInteractionEnabled = NO;
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
self.userInteractionEnabled = YES;
//选中的视图置顶居中
self.centerView.transform = CGAffineTransformMakeRotation(-(self.selectedBtn.tag * M_PI / 6));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self startAnimation];//1秒之后开始旋转
});
}
@end
#import <UIKit/UIKit.h>
@interface WButton : UIButton
@end
#import "WButton.h"
@implementation WButton
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageW = 40;
CGFloat imageH = 47;
CGFloat imageX = (contentRect.size.width - imageW) * 0.5;
CGFloat imageY = 20;
return CGRectMake(imageX, imageY, imageW, imageH);
}
- (void)setHighlighted:(BOOL)highlighted
{
}
@end
使用
#import "ViewController.h"
#import "WheelView.h"
@interface ViewController ()
- (IBAction)stop:(id)sender;
@property (nonatomic, strong) WheelView *wheel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
WheelView *wheel = [WheelView wheelView];
wheel.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
[wheel startAnimation];
[self.view addSubview:wheel];
self.wheel = wheel;
}