轉盤功能的詳細實現
來源:程序員人生 發布時間:2014-12-13 08:57:14 閱讀次數:2638次
轉盤功能的詳細實現
1、自定義轉盤XIB
+ (instancetype)wheelView
{
return [[NSBundle
mainBundle]
loadNibNamed:@"HMWheelView"
owner:nil
options:nil][0];
}
2、添加按鈕
#warning
添加按鈕
- (void)awakeFromNib
{
// 加載完XIB就開始啟動轉盤
self.link.paused
=
NO;
_rotationView.userInteractionEnabled
=
YES;
//
裁剪的大圖片
UIImage *bigImage = [UIImage
imageNamed:@"LuckyAstrology"];
UIImage *selectedImage = [UIImage
imageNamed:@"LuckyAstrologyPressed"];
//
圖片的尺寸
CGFloat imageW =
40
* [UIScreen
mainScreen].scale;
CGFloat imageH =
47
* [UIScreen
mainScreen].scale;
for (int
i =
0; i <
12; i++) {
//
創建按鈕
HMWheelButton *button = [HMWheelButton
buttonWithType:UIButtonTypeCustom];
//
錨點
button.layer.anchorPoint
=
CGPointMake(0.5,
1);
//
位置
button.layer.position
=
CGPointMake(self.bounds.size.width
*
0.5,
self.bounds.size.height
*
0.5);
//
旋轉按鈕
button.layer.transform
=
CATransform3DMakeRotation(angle2radian(i
*
30),
0,
0,
1);
//
尺寸
button.bounds
=
CGRectMake(0,
0,
68,
143);
//
設置選中時候的背景圖片
[button
setBackgroundImage:[UIImage
imageNamed:@"LuckyRototeSelected"]
forState:UIControlStateSelected];
//
設置按鈕的圖片
// image:裁剪的圖片
// rect:裁剪的尺寸
CGRect clipRect =
CGRectMake(i * imageW,
0, imageW, imageH);
CGImageRef smallImage =
CGImageCreateWithImageInRect(bigImage.CGImage,
clipRect);
[button
setImage:[UIImage
imageWithCGImage:smallImage]
forState:UIControlStateNormal];
//
設置選中的圖片
CGImageRef selectedSmallImage =
CGImageCreateWithImageInRect(selectedImage.CGImage,
clipRect);
[button
setImage:[UIImage
imageWithCGImage:selectedSmallImage]
forState:UIControlStateSelected];
//
監聽點擊事件
[button
addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchDown];
// 默許選中第1個
if (i ==
0) {
[self
btnClick:button];
}
[_rotationView
addSubview:button];
}
}
3、實現監聽方法
#warning
監聽按鈕點擊
- (void)btnClick:(UIButton
*)button
{
_selectedButton.selected
=
NO;
button.selected
=
YES;
_selectedButton
= button;
}
4、定義定時器
- (CADisplayLink
*)link
{
if (_link
==
nil) {
CADisplayLink *link = [CADisplayLink
displayLinkWithTarget:self
selector:@selector(update)];
[link
addToRunLoop:[NSRunLoop
mainRunLoop]
forMode:NSDefaultRunLoopMode];
_link = link;
}
return
_link;
}
- (void)update
{
_rotationView.transform
=
CGAffineTransformRotate(_rotationView.transform, ((45 / 60.0)
/
180.0
* M_PI));
}
5、實現開始選號功能
- (IBAction)start:(id)sender
{
// 1.不要和用戶交互
_rotationView.userInteractionEnabled
=
NO;
// 2.取消漸漸的旋轉
_link.paused
=
YES;
CABasicAnimation
*anim = [CABasicAnimation
animation];
anim.keyPath
=
@"transform.rotation";
anim.toValue
=
@(M_PI
*
2 *
3);
anim.duration
=
0.5;
anim.delegate
=
self;
[_rotationView.layer
addAnimation:anim
forKey:nil];
}
6、實現 CAAnimationDelegate的代理方法
- (void)animationDidStop:(CAAnimation
*)anim finished:(BOOL)flag
{
_rotationView.userInteractionEnabled
=
YES;
//
讓選中按鈕回到最在上面的中間位置:
CGFloat angle =
atan2(_selectedButton.transform.b,
_selectedButton.transform.a);
//
把我們的轉盤反向旋轉這么多°
_rotationView.transform
=
CGAffineTransformMakeRotation(-angle);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(2
*
NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[self
startRotating];
});
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈