最近学习了一个关于转盘的动画,现将其中一些知识点整理出来,算是一些总结,等将来有时间回顾回顾
//大图片,这是原图
UIImage *img = [UIImage imageNamed:@"LuckyAstrology"];
UIImage *imgSelected = [UIImage imageNamed:@"LuckyAstrologyPressed"];
//从图片中裁剪对应星座的图片,因为图片有十二星座,现将其十二等分分别设给十二个button
CGFloat smallW = img.size.width / 12 * [UIScreen mainScreen].scale;//由于CGImageCreateWithImageInRect方法只认像素,所以乘以其屏幕的scale,在Retina屏上为2,否则为1
CGFloat smallH = img.size.height * [UIScreen mainScreen].scale;
for (int i = 0; i < 12; i++) {
//在这里自定义button,重写其下面方法,将其按钮图片居于中间
/*
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat imageW = 40;
CGFloat imageH = 47;
CGFloat imageX = (contentRect.size.width - imageW)/2;
CGFloat imageY = 20;
return CGRectMake(imageX, imageY, imageW, imageH);
}
*/
CLButton *btn = [CLButton buttonWithType:UIButtonTypeCustom];
CGRect smallRect = CGRectMake(smallW * i, 0, smallW, smallH);
// CGImageCreateWithImageInRect只认像素
CGImageRef smallImage = CGImageCreateWithImageInRect(img.CGImage, smallRect);//从方法名即可看出这是创建一个CGImage,前面参数是你原图的CGImage,后面是你的截取的大小
[btn setImage:[UIImage imageWithCGImage:smallImage] forState:UIControlStateNormal];
CGImageRef smallImageSelected = CGImageCreateWithImageInRect(imgSelected.CGImage, smallRect);
[btn setImage:[UIImage imageWithCGImage:smallImageSelected] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
btn.bounds = CGRectMake(0, 0, 68, 143);
//这里设置anchorPoint与position,根据其frame.origin.x = position.x-anchorPoint*btn.size.width从而确定其x,y点
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 * i) / 180.0 * M_PI;
btn.transform = CGAffineTransformMakeRotation(angle);//将btn按某一个角度旋转,围成一个圆形
这里顺便说一下CADisplayLink这个类,这个类的用法与NSTimer类用法相似,用类方法创建出一个对象
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(refresh)];//这个方法一秒钟会刷新60帧,相当于调用60次方法
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];