IOS开发基础之大转盘案例

IOS开发基础之大转盘案例

本案例使用xib加载的,以及核心动画的使用。动态按钮的使用和创建。等知识。
在这里插入图片描述

在这里插入图片描述

源码在我的主页资源下。欢迎下载。
核心代码在这。

//  LJRotateView.h
//  37-大转盘
//
//  Created by 鲁军 on 2021/2/27.
//
#import <UIKit/UIKit.h>
@interface LJRotateView : UIView
+(instancetype)rotateView;
-(void)startRotate;
@end
//
//  LJRotateView.m
//  37-大转盘
//
//  Created by 鲁军 on 2021/2/27.
//

#import "LJRotateView.h"

@interface LJRotateView () <UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *rotateImage; //锯齿图片
@property(nonatomic,weak)UIButton *currentButton;

@property(nonatomic,strong)CADisplayLink *link;
- (IBAction)pickNumber:(id)sender;



@end
@implementation LJRotateView

-(void)startRotate{
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotate)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    self.link =link;
}
//1 秒调用60s
-(void)rotate{
    
   // NSLog(@"");
    self.rotateImage.transform= CGAffineTransformRotate(self.rotateImage.transform, 2*M_PI/60/10);
    
    
}

+ (instancetype)rotateView{
     return [[NSBundle mainBundle] loadNibNamed:@"LJRotateView" owner:nil options:nil][0];
}
- (void)awakeFromNib{
   [super awakeFromNib];
    //设置父控件可交互
    self.rotateImage.userInteractionEnabled = YES;
    
    for(int i=0;i<12;++i){
      //  UIButton *btn = [[UIButton alloc] init];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag=i;
      //  btn.backgroundColor = [UIColor redColor];
        UIImage *image = [UIImage imageNamed:@"LuckyAstrology"];
        image = [self clipImageWithImage:image withIndex:i];
        [btn setImage:image forState:UIControlStateNormal];
        
        UIImage *imagePress = [UIImage imageNamed:@"LuckyAstrologyPressed"];
        imagePress = [self clipImageWithImage:imagePress withIndex:i];
        [btn setImage:imagePress  forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
        
        //btn内部 imageView 往上偏移
        [btn setImageEdgeInsets:UIEdgeInsetsMake(-50, 0, 0, 0)];
      
        btn.userInteractionEnabled=YES;
        //btn.isUserInteractionEnabled=YES;
        
        
        [self.rotateImage addSubview:btn];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)btnClick:(UIButton *)sender{
  
    self.currentButton.selected = NO;
    sender.selected = YES;
    
    self.currentButton = sender;
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    for (int i=0; i<self.rotateImage.subviews.count; ++i) {
        UIButton *btn = self.rotateImage.subviews[i];
        btn.frame = CGRectMake(0, 0, 68, 143);
        btn.center = self.rotateImage.center;
        //锚点
        btn.layer.anchorPoint = CGPointMake(0.5, 1);
        //计算一个按钮的夹角
        CGFloat angle = 2*M_PI / 12;
        btn.transform = CGAffineTransformMakeRotation(angle*i);
    }
}
-(UIImage*)clipImageWithImage:(UIImage *)image withIndex:(NSInteger)index{
    //计算 rect
    CGFloat w=image.size.width / 12*[UIScreen mainScreen].scale;
    CGFloat h=image.size.height*[UIScreen mainScreen].scale;
    CGFloat x=index * w;
    CGFloat y=0;
    CGImageRef  imageRef = CGImageCreateWithImageInRect(image.CGImage,CGRectMake(x, y, w, h));
    return [[UIImage alloc] initWithCGImage:imageRef scale:2.3 orientation:UIImageOrientationUp];
}
- (IBAction)pickNumber:(id)sender {
    
    
    if([self.rotateImage.layer animationForKey:@"keys"]){
        return;
        //如果存在,就return掉,后面的都不执行
    }
    
    self.link.paused = YES;
    
    
    CABasicAnimation *anim=[[CABasicAnimation alloc] init];
    
    anim.keyPath = @"transform.rotation";
    
    //计算 需要减去的角度
    CGFloat angle =2*M_PI / 12 *self.currentButton.tag;
    
    anim.toValue = @(5*M_PI*2 - angle);//圈
    anim.duration = 3;//设置时间
    anim.fillMode =kCAFillModeForwards;
    anim.removedOnCompletion=NO;
    [self.rotateImage.layer addAnimation:anim forKey:@"keys"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(anim.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.rotateImage.transform = CGAffineTransformMakeRotation(-angle);
  
    
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"温馨提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        
       /* UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"12332" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
       
        
        [alert1 show];
        */
        
        //移除核心动画
        [self.rotateImage.layer removeAnimationForKey:@"keys"];
        
    });
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    self.link.paused = NO;
    
    
}
@end

//
//  ViewController.m
//  37-大转盘
//
//  Created by 鲁军 on 2021/2/27.
//

#import "ViewController.h"
#import "LJRotateView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.view.layer.contents=(__bridge id)[UIImage imageNamed:@"LuckyBackground"].CGImage;
    
    LJRotateView *rotateView = [LJRotateView rotateView];
    
    rotateView.center=self.view.center;
    
    [self.view addSubview:rotateView];
    
  //  [rotateView startRotate];
    
    
}


- (BOOL)prefersStatusBarHidden{
    return YES;
}

@end

已标记关键词 清除标记
相关推荐
<p> <b><span style="background-color:#FFE500;">【超实用课程内容】</span></b> </p> <p> <br /> </p> <p> <br /> </p> <p> 本课程内容包含讲解<span>解读Nginx的基础知识,</span><span>解读Nginx的核心知识、带领学员进行</span>高并发环境下的Nginx性能优化实战,让学生能够快速将所学融合到企业应用中。 </p> <p> <br /> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><br /> </b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><span style="background-color:#FFE500;">【课程如何观看?】</span></b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> PC端:<a href="https://edu.csdn.net/course/detail/26277"><span id="__kindeditor_bookmark_start_21__"></span></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 移动端:CSDN 学院APP(注意不是CSDN APP哦) </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 本课程为录播课,课程永久有效观看时长,大家可以抓紧时间学习后一起讨论哦~ </p> <p style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <br /> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <strong><span style="background-color:#FFE500;">【学员专享增值服务】</span></strong> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b>源码开放</b> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化 </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 下载方式:电脑登录<a href="https://edu.csdn.net/course/detail/26277"></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a>,播放页面右侧点击课件进行资料打包下载 </p> <p> <br /> </p> <p> <br /> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页