日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发基础之大转盘案例

發(fā)布時間:2023/12/18 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发基础之大转盘案例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

IOS開發(fā)基礎(chǔ)之大轉(zhuǎn)盤案例

本案例使用xib加載的,以及核心動畫的使用。動態(tài)按鈕的使用和創(chuàng)建。等知識。

源碼在我的主頁資源下。歡迎下載。
核心代碼在這。

// LJRotateView.h // 37-大轉(zhuǎn)盤 // // Created by 魯軍 on 2021/2/27. // #import <UIKit/UIKit.h> @interface LJRotateView : UIView +(instancetype)rotateView; -(void)startRotate; @end // // LJRotateView.m // 37-大轉(zhuǎn)盤 // // 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 秒調(diào)用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];//設(shè)置父控件可交互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內(nèi)部 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;//錨點(diǎn)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{//計算 rectCGFloat 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掉,后面的都不執(zhí)行}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;//設(shè)置時間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-大轉(zhuǎn)盤 // // 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 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的IOS开发基础之大转盘案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。