- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 UIBezierPath
开发自定义循环进度条。我想根据随机生成的 CGFloat 值更改进度。
我的进度条是这样的:
我使用以下代码绘制了进度条:
// Draw the arc with bezier path
int radius = 100;
CAShapeLayer *arc = [CAShapeLayer layer];
arc.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 50) radius:radius startAngle:M_PI endAngle:M_PI/150 clockwise:YES].CGPath;
arc.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
arc.cornerRadius = 100.0;
arc.fillColor = [UIColor clearColor].CGColor;
arc.strokeColor = [UIColor purpleColor].CGColor;
arc.lineWidth = 6;
[self.view.layer addSublayer:arc];
// Animation of the progress bar
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 5.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = YES; // Remain stroked after the animation..
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:10.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[arc addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
// Gradient of progress bar
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.view.frame;
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,(__bridge id)[UIColor purpleColor].CGColor ];
gradientLayer.startPoint = CGPointMake(0,0.1);
gradientLayer.endPoint = CGPointMake(0.5,0.2);
[self.view.layer addSublayer:gradientLayer];
gradientLayer.mask = arc;
refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 370, 50, 50)];
[refreshButton addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
[refreshButton setBackgroundImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
[self.view addSubview:refreshButton];
带有绿色箭头的 UIButton 会随机生成 CGFloat 值。所以我的问题是如何重置动画,使其在0.0和1.0之间时停在相应的位置?我试图在按钮的方法中插入动画代码,但结果新弧线绘制在现有弧线之上。
-(void)refresh:(id)sender{
NSLog(@"Refresh action:");
CGFloat randomValue = ( arc4random() % 256 / 256.0 );
NSLog(@"%f", randomValue);
valueLabel.text = @"";
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 370, 100, 50)];
valueLabel.text = [NSString stringWithFormat: @"%.6f", randomValue];
[self.view addSubview:valueLabel];
// Draw the arc with bezier path
int radius = 100;
CAShapeLayer *arc = [CAShapeLayer layer];
arc.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 50) radius:radius startAngle:M_PI endAngle:M_PI/150 clockwise:YES].CGPath;
arc.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
arc.cornerRadius = 100.0;
arc.fillColor = [UIColor clearColor].CGColor;
arc.strokeColor = [UIColor purpleColor].CGColor;
arc.lineWidth = 6;
[self.view.layer addSublayer:arc];
// Animation of the progress bar
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 5.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = YES; // Remain stroked after the animation..
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:randomValue];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[arc addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}
我不知道如何重新绘制弧线,所以任何帮助或想法将不胜感激!
非常感谢。
花岗岩
最佳答案
尝试重新使用子层或替换它。使 arc 成为类变量。然后循环遍历子层,移除旧弧,然后使用新的随机 float 百分比重新制作它。一旦 arc 是一个类变量,那么您可以替换它并从零开始设置动画,或者您可以从它的当前百分比到新百分比设置动画。
关于ios - 根据随机 CGFloat 值重新绘制 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636949/
我试图随机生成 CGFloat 但不知道它是如何在 swift 3 中完成的。什么是 CGFloat.random(min: CGFloat, max: CGFloat) 相当于 swift 2 的
我收到一个错误,我不知道如何修复。错误是: Sending 'CGFloat' (aka 'float') to parameter of incompatible type 'CGFloat *'
我有一个自动布局便利功能,可以将 View 堆叠在彼此之上。对于我的间距参数,我想允许: View 之间的间距为零 或单个CGFloat(所有 View 均匀分布) 或 CGFloat 数组( Vie
尝试设置 CAShapeLayer 的 zPosition 时出现错误。这是代码: self.view.layer.sublayers[l].zPosition = CGFloat(1) 出于某种原因
我有一个像 CGFloat 数组 [[[1,2],[2,3]],[[6,1],[5,6]],[[6,1],[11,6],[21,3]]] 不,我想合并子数组并在每个子数组中添加数组数据,如 [[1,2
我有这个代码: let startX = CGFloat(arc4random_uniform(maxX + (quarterX*2))) - quarterX quarterX 是一个 CGFloa
我在 XCode 6 beta 4 上使用 swift 时遇到问题,这让我发疯 我正在 IOS 8 中开发游戏并且我遵循了教程但我得到了这个:Float is not convertible to C
我正在尝试使用 CGRect.insetBy( dx : ... , dy : ...) 但是代码给了我一个根本无法解决的错误。 func scaleRect (rect: CGRect , xSca
我不太理解这个编译错误,因为我没有得到这个错误并且可以在真实设备上编译和运行,但是如果我尝试在模拟器上构建和运行我会得到这个错误。 Cannot invoke initializer for type
我有一个简单的例子,我尝试画一个圆。下面这段代码没有给我一个圆圈。 import UIKit class PlayingCardView: UIView { override func dra
我正在尝试转换使用 Facebook's pop library 的 ObjC 类 swift 。 Pop 使用了相当多的 C。 在 ObjC 中,我有一个看起来像这样的 block ...... p
我必须将我的整数包装在 CGFloat() 中才能在 Swift 2.3 中编译 如果我只执行 * 2,那么它会编译。为什么会这样?这在 Swift 3 中已修复吗? var multiplier =
我正在尝试创建一个带有CGFloat参数并返回CGFloat的块变量。 CGFloat (^debt)(CGFloat) = ^(CGFloat myFloat) { return myFloa
label.position = CGPointMake(self.size.width*0.125, self.size.height-CGFloat(0.15)*self.size.height*
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
如果这说明了问题,这里是原始的 Objective-C 代码。 int x = (arc4random()%(int)(self.gameView.bounds.size.width*5)) - (i
我正在尝试将 Float 值转换为 CGFloat 值,这里是我的代码: let myFloatForR = 0 let r = CGFloat(myFloatForR) le
我是 sprite kit 2d 游戏开发的新手。现有的 swift2 项目运行良好,但不幸的是更新 swift4 后流动代码出现错误。我该如何解决这个问题 class SGScene : SKSce
我的 .h 类中有以下内容: extern CGFloat tableMarginSide; extern CGFloat tableMarginTop; extern CGFloat fromIma
在我的 CGFloat 包装器中,我收到一个错误 Returning CGFloat (aka Double) from a function result type CGfloat aka doub
我是一名优秀的程序员,十分优秀!