今天将画圆和动画练习联系在一起,一起加强这方面的练习。在画圆的时候,发现一些小瑕疵。今天主要想弄一个圆周运动,这个其实在flash做得很多。今天尝试一下用oc来练习。
首先准备一些工作,前提是画一个填充圆和画一个边框圆形。
第一步:创建Ball.h 和Ball.m
第二步:创建Circle.h和Circle.m
#import "Ball.h"
@implementation Ball
- (void)drawRect:(CGRect)rect
{
//画一个圆
CGContextRef context = UIGraphicsGetCurrentContext();//拿到上下文
CGRect cRect = CGRectMake(0, 0, 30, 30);
CGContextSetLineWidth(context, 2); //设置线条粗细
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextAddEllipseInRect(context, cRect);//绘制一个圆
CGContextFillPath(context);//关键一步填充
}
@end
同样Circle里面画一个160x160大小的圆。
#import "Circle.h"
@implementation Circle
- (void)drawRect:(CGRect)rect
{
//画一个圆
CGContextRef context = UIGraphicsGetCurrentContext();//拿到上下文
CGRect cRect = CGRectMake(0, 0, 160, 160);
CGContextSetLineWidth(context, 1); //设置线条粗细
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextAddEllipseInRect(context, cRect);//绘制一个圆
CGContextStrokePath(context);
}
@end
在viewController.m添加如下代码
//
// ViewController.m
// donghua_example
//
// Copyright (c) 2015年 xiashu. All rights reserved.
//
#import "ViewController.h"
#import "Ball.h"
#import "Circle.h"
@interface ViewController ()
@property (nonatomic,strong) Ball *ball;
@property (nonatomic,strong) Circle *circle;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic) CGFloat angle;//角度
@property (nonatomic) CGPoint point;//坐标点
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.circle = [[Circle alloc]initWithFrame:CGRectMake(0, 0, 160, 160)];
self.circle.center = self.view.center;
self.circle.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.circle];
self.ball = [[Ball alloc]init];
self.ball.frame = CGRectMake(0, 0, 40, 40);
self.ball.backgroundColor =[UIColor clearColor];
self.ball.center = self.view.center;
self.ball.clipsToBounds = YES;
[self.view addSubview:self.ball];
[self.ball setNeedsDisplay];
self.point = self.view.center;
self.angle = 0;
self.timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(makeAnimation) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
-(void) makeAnimation
{
self.angle+=5;
CGFloat x = self.point.x+ 80* cos(self.angle * M_PI/180);
CGFloat y = self.point.y+ 80* sin(self.angle * M_PI/180);
self.ball.center = CGPointMake(x, y);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
完成后一个动画就完成了。
今天无意之中发现一款录制gif工具,简单实用,仅仅两三步就可以录制出出来。(GifCam) 简单微小,因为使用虚拟机的缘故,没能在mac 找到一款好用,这款正好满足我需求。GifCam