ios - 我怎样才能使线路转弯?

标签 ios objective-c iphone xcode cabasicanimation

我画了一条像时钟一样转动的线。

这是我理想中的行进

this is the line's moving of my ideal

但是我的代码是这样移动的

but my code works like this

我怎样才能像第二张图片那样画一条线?

这是我的代码

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()

layerLine = [CALayer layer];
[layerLine setFrame:CGRectMake(0, 0,2, 90)];
[layerLine setBackgroundColor:[UIColor yellowColor].CGColor];
[self.view.layer addSublayer:layerLine];
CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, ([[UIScreen mainScreen] bounds].size.height/2)-(layerLine.frame.size.height/4));
layerLine.anchorPoint = CGPointMake(0, 0);
layerLine.position = centerPoint;
layerLine.transform = CATransform3DMakeRotation(M_PI_2*2, 0.0, 0.0, 1.0);
if (!timer)
{
  timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(onTickTick) userInfo:nil repeats:YES];
}

定时器调用方法

-(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);
}

关于ios - 我怎样才能使线路转弯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223046/

相关文章:

ios - 错误域=NSCocoaErrorDomain 代码=3840 "The operation couldn’ 使用 AFNetworking 完成

ios - Accelerate/Scenekit Framework::3d 和 4d 向量的数学矩阵/向量库在哪里?

ios - UITableView 是否有一个包含所有单元格的数组属性?

objective-c - 如何禁用 NSStatusItem 的高亮模式

iphone - 在 iPhone 中修改 MWPhotoBrowser

ios - 在 iOS 应用程序中添加自动布局 subview 时高度错误

ios - 显示 UIPickerView 文本框被选中,选中后隐藏

ios - 无法编辑瓷砖 map Xcode 8 GM

ios - 无效的产品标识符 : How do I verify iOS sandbox environment

iphone - 如何实现像 iPhone 联系人应用程序中那样的表格 View 单元格的列 - 添加新联系人 View ?