iOS-轉盤

一.自定義轉盤按鈕

//  WheelBtn.h

#import <UIKit/UIKit.h>

@interface WheelBtn : UIButton

@end
//  WheelBtn.m

#import "WheelBtn.h"

@implementation WheelBtn


-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
   CGRect rect =  CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height * 0.5);
    
    if (CGRectContainsPoint(rect, point)) {
        //在指定的範圍內
        return [super hitTest:point withEvent:event];
    }else {
        return nil;
    }
    
}





//返回當前按鈕當中ImageView的位置尺寸
//contentRect:當前按鈕的位置尺寸
-(CGRect)imageRectForContentRect:(CGRect)contentRect {
    
    CGFloat w = 40;
    CGFloat h=  48;
    CGFloat x = (contentRect.size.width - w) * 0.5;
    CGFloat y = 20;
   
   return  CGRectMake(x, y, w, h);
}


//返回當前按鈕當中Label的位置尺寸
//-(CGRect)titleRectForContentRect:(CGRect)contentRect {
//
//}



//取消按鈕高亮狀態下做的事
-(void)setHighlighted:(BOOL)highlighted {
    
}



@end

二.使用xib自定義轉盤View
//  WheelView.h

#import <UIKit/UIKit.h>

@interface WheelView : UIView

//快速的創建一個轉盤
+ (instancetype)wheelView;

//讓轉盤開始旋轉
- (void)rotation;

//讓轉盤暫停旋轉
- (void)stop;

@end
//  WheelView.m

#import "WheelView.h"
#import "WheelBtn.h"
#define angle2Rad(angle) ((angle) / 180.0 * M_PI)

@interface WheelView()

@property (weak, nonatomic) IBOutlet UIImageView *contentV;


/** 定時器 */
@property (nonatomic, strong) CADisplayLink *link;

//當前選中的按鈕
@property (nonatomic, weak) UIButton *selectBtn;


@end


@implementation WheelView



-(CADisplayLink *)link  {
    if (_link == nil) {
        
        //添加定時器,保持一直旋轉
        CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
        [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        _link = link;
        
    }
    return _link;
}

+ (instancetype)wheelView {
    
  return  [[[NSBundle mainBundle] loadNibNamed:@"WheelView" owner:nil options:nil] lastObject];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"WheelView" owner:nil options:nil] lastObject];
    }
    return self;
}

-(void)awakeFromNib {
    
    
    
    self.contentV.userInteractionEnabled = YES;
    //添加轉盤的按鈕
    CGFloat btnW = 68;
    CGFloat btnH = 143;
    CGFloat angle = 0;
    
    //加載原始大圖片
    UIImage *oriImage = [UIImage imageNamed:@"LuckyAstrology"];
    //加載原始選中的大圖片
    UIImage *oriSelImage = [UIImage imageNamed:@"LuckyAstrologyPressed"];
    
    NSLog(@"%@",NSStringFromCGSize(oriImage.size));
    
    CGFloat X = 0;
    CGFloat Y = 0;
    CGFloat clipW = oriImage.size.width / 12.0 * 2;
    CGFloat clipH = oriImage.size.height * 2;
    
    
    for(int i = 0; i < 12; i++) {
        WheelBtn *btn = [WheelBtn buttonWithType:UIButtonTypeCustom];
        btn.bounds = CGRectMake(0, 0, btnW, btnH);
        //[btn setBackgroundColor:[UIColor redColor]];
        
        //設置按鈕選中狀態下的背景圖片
        [btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
        
        
        //給定一張圖片,截取指定區域範圍內的圖片,
        X = i * clipW;
        
        //CGImageCreateWithImageInRect,使用的座標都是以像素點,
        //在ios當中使用的都是點座標.
       CGImageRef clipImage =  CGImageCreateWithImageInRect(oriImage.CGImage, CGRectMake(X, Y, clipW, clipH));
        //設置按鈕正常狀態下顯示的圖片
        [btn setImage:[UIImage imageWithCGImage:clipImage] forState:UIControlStateNormal];
        
        CGImageRef clipSelImage =  CGImageCreateWithImageInRect(oriSelImage.CGImage, CGRectMake(X, Y, clipW, clipH));
        //設置按鈕選中狀態下顯示的圖片
        [btn setImage:[UIImage imageWithCGImage:clipSelImage] forState:UIControlStateSelected];
        
        
        
        //設置按鈕位置
        btn.layer.anchorPoint = CGPointMake(0.5, 1);
        btn.layer.position = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);

        
        //讓第一個按鈕在上一個基礎上面旋轉30.
        btn.transform = CGAffineTransformMakeRotation(angle2Rad(angle));
        angle += 30;
        
        //監聽按鈕的點擊
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentV addSubview:btn];
        
        //默認讓第一個成爲選中狀態
        if (i == 0) {
            [self btnClick:btn];
        }

        
    }
}


- (void)btnClick:(UIButton *)btn {
    
    //讓當前點擊的按鈕成爲選中狀態
    
    //1.讓當前選中的按鈕取消選中.
    self.selectBtn.selected = NO;
    //2.讓當前點擊的按鈕成爲選中狀態
    btn.selected = YES;
    //3.當前點擊的按鈕成爲選中狀態
    self.selectBtn = btn;
    
}


//讓轉盤開始旋轉
- (void)rotation {

    self.link.paused = NO;
    
//    CABasicAnimation *anim = [CABasicAnimation animation];
//    anim.keyPath = @"transform.rotation";
//    anim.toValue = @(M_PI * 3);
//    anim.duration = 5;
//    anim.repeatCount = MAXFLOAT;
//    
//    [self.contentV.layer addAnimation:anim forKey:nil];
    
}


//讓轉盤暫停旋轉
- (void)stop {
    self.link.paused = YES;
}


- (void)update {
    
    self.contentV.transform = CGAffineTransformRotate(self.contentV.transform, M_PI / 300.0);
}

//開始選號
- (IBAction)startChoose:(id)sender {
    
    //讓轉盤快速的旋轉幾圈,
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"transform.rotation";
    anim.toValue = @(M_PI * 4);
    anim.duration = 0.5;
    anim.delegate = self;
    [self.contentV.layer addAnimation:anim forKey:nil];
    
    //動畫結束時當前選中的按鈕指向最上方

}

//當動畫結束時調用
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    //動畫結束時當前選中的按鈕指向最上方
    //讓當前選中的按鈕的父控件倒着旋轉回去.
    
    //獲取當前選中按鈕旋轉的角度
    CGAffineTransform transform = self.selectBtn.transform;
    //通過transform獲取當前旋轉的度數
    CGFloat angle = atan2(transform.b, transform.a);
    
    NSLog(@"%f",angle);
    
    
    self.contentV.transform = CGAffineTransformMakeRotation(-angle);
    
}
@end
xib中文件如下圖:


三.控制器.m文件如下

//  ViewController.m

#import "ViewController.h"
#import "WheelView.h"

@interface ViewController ()


@property (nonatomic, weak) WheelView *wheelV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    WheelView *wheelV = [WheelView wheelView];
    wheelV.center = self.view.center;
    self.wheelV  = wheelV;
    [self.view addSubview:wheelV];
    
//    WheelView *wheelV = [[WheelView alloc] init];
//    wheelV.center = self.view.center;
//    self.wheelV = wheelV;
//    [self.view addSubview:wheelV];

}

- (IBAction)rotation:(id)sender {
    [self.wheelV rotation];
}

- (IBAction)stop:(id)sender {
    [self.wheelV stop];
}

@end
運行結果如下:











發佈了4 篇原創文章 · 獲贊 1 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章