ios - 改变动画的位置?

标签 ios objective-c

我正在使用我从之前的 stackOverFlow 帖子修改的代码。我想更改动画的位置,但是当我运行时它不会更改到我想要的位置。相反,它将远离一些像素。基本上我想要的是我想输入一个介于 480-0 和 320-0 之间的 X 值或 Y,并使用 circle.position 将动画移动到该位置。我该怎么做?

-(CAShapeLayer *)addCircleYellownWithRadius:(float)radius5 withduration:(float)duration X:(float)x Y:(float)y{
    int radius = (int)radius5;
    CAShapeLayer *circle = [CAShapeLayer layer];
    // Make a circular shape
    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                             cornerRadius:radius].CGPath;
    // Center the shape in self.view
    circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
                                  CGRectGetMidY(self.view.frame)-radius);



    // Configure the apperence of the circle
    circle.fillColor = [UIColor clearColor].CGColor;
    circle.strokeColor = [UIColor whiteColor].CGColor;
    circle.lineWidth = 5;

    // Add to parent layer
    [self.view.layer addSublayer:circle];

    // Configure animation
    CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    drawAnimation.duration            = duration; // "animate over 10 seconds or so.."
    drawAnimation.repeatCount         = 1.0;  // Animate only once..

    // Animate from no part of the stroke being drawn to the entire stroke being drawn
    drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

    // Experiment with timing to get the appearence to look the way you want
    drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    // Add the animation to the circle
    [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
    return circle;
}

最佳答案

替换,

circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
                                  CGRectGetMidY(self.view.frame)-radius);

与,

circle.position = CGPointMake(x-radius, y-radius);

关于ios - 改变动画的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230161/

相关文章:

ios - 互操作 Async/await、@MainActor 和 DispatchQueue.main.async

ios - 如何暂停调用tableview的reloadData?

objective-c - 使用 avaudioplayer、UITableView NSURL 加载和播放音频

ios - 在iOS的保管箱中未获取文件大小

IOS popviewcontroller去除导航栏

ios - 屏蔽 CAShapeLayer 会产生这个奇怪的工件?

XCode 在调试时自动删除 plist

ios - Swift - 限制 UITextview 行中的字符数

ios - 如何在ios中将图像添加为圆形框架

ios - 调用自定义 Segue 类执行方法,操作不执行任何操作