Ios 我怎样才能使线路转弯?
我画了一条像时钟一样旋转的线 这是我理想的一条线 但我的代码是这样运行的 我怎样才能画出第二幅画的线条 这是我的密码Ios 我怎样才能使线路转弯?,ios,objective-c,iphone,xcode,cabasicanimation,Ios,Objective C,Iphone,Xcode,Cabasicanimation,我画了一条像时钟一样旋转的线 这是我理想的一条线 但我的代码是这样运行的 我怎样才能画出第二幅画的线条 这是我的密码 LineLayer.m - (void)drawInContext:(CGContextRef)ctx { CGMutablePathRef path = CGPathCreateMutable(); CGPoint points[] = { CGPointMake(160, 220),
LineLayer.m
- (void)drawInContext:(CGContextRef)ctx
{
CGMutablePathRef path = CGPathCreateMutable();
CGPoint points[] = {
CGPointMake(160, 220),
CGPointMake(160, 120),
};
int numPoints = sizeof(points)/sizeof(points[0]);
CGPathAddLines(path, NULL, points, numPoints);
CGContextAddPath(ctx, path);
CGPathRelease(path);
UIColor *lineColor = [UIColor colorWithWhite:1.0 alpha:1.0];
CGContextSetStrokeColorWithColor(ctx, lineColor.CGColor);
CGContextSetLineWidth(ctx, 5.0);
CGContextDrawPath(ctx, kCGPathStroke);
}
ViewController.h
- (void)setup
{
LineLayer *linelayer = [LineLayer layer];
linelayer.frame = CGRectInset(self.view.bounds,0.0,0.0);
linelayer.anchorPoint = CGPointMake(0.5, 0.5);
[self.view.layer addSublayer:linelayer];
[linelayer setNeedsDisplay];
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.toValue = [NSNumber numberWithFloat:M_PI / 2.0];
animation.duration = 0.5;
animation.repeatCount = MAXFLOAT;
animation.cumulative = YES;
[linelayer addAnimation:animation forKey:@"ImageViewRotation"];
}
你需要一个时钟秒针之类的东西
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
NSTimer *timer1;//global declaration
CALayer *layerLine;//global declaration
在viewDidLoad()中
定时器调用方法
-(void)onTickTick
{
static int i = 180;
if (i==540)//if u change the value for i, it may affect this condition
{
i=180;
}
i=i+6;//u can replace 6 with 30 or any other value as u require
layerLine.transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(i), 0.0, 0.0, 1);
}
您需要绘制圆弧而不是简单的直线,您需要使用CGContextAddArcToPoint
并检查(此)[
-(void)onTickTick
{
static int i = 180;
if (i==540)//if u change the value for i, it may affect this condition
{
i=180;
}
i=i+6;//u can replace 6 with 30 or any other value as u require
layerLine.transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(i), 0.0, 0.0, 1);
}