• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 从屏幕中心扩大一个圆圈

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:08:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试将圆从零半径扩展到屏幕中心的预定义半径。代码如下

在 viewDidLoad 中:-

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [super viewDidLoad];

    circleRadius = 0.0f;

    circle = [CAShapeLayer layer];

    [circle setAnchorPoint:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
    [circle setFillColor:[UIColor redColor].CGColor];
    [circle setStrokeColor:[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor];
    [circle setLineWidth:0.0f];

    [self.view.layer addSublayer:circle];


    [self drawCircleWithRadius:circleRadius];


}

- (void)animateImageView {
    circleRadius += 70.0f;
    [self drawCircleWithRadius:circleRadius];
}

- (void)drawCircleWithRadiusCGFloat)radius {

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath"path"];
    [pathAnimation setFromValueid)circle.path];//(id)circle.path
    [pathAnimation setToValueid)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0f * radius, 2.0f * radius) cornerRadius:radius].CGPath];
    [pathAnimation setDuration:0.5f];
    [pathAnimation setRepeatCount:1.0f];
    [pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath;
    circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius, CGRectGetMidY(self.view.frame)-radius);
    [circle addAnimation:pathAnimation forKey"changePathAnimation"];
}

-animateImageView 在点击条形按钮时运行。 不知何故,动画不那么流畅,圆圈似乎没有从屏幕中心长出来。

请指出代码中的错误。 谢谢。



Best Answer-推荐答案


试试下面的代码,这对我有用。我认为问题在于设置正确的路径。那么请问我是如何计算下面代码中的路径的。

- (void)viewDidLoad
{
  [super viewDidLoad];

  circleRadius = 200.0f;
  CGPoint ptCenter = self.view.center;

  circle = [CAShapeLayer layer];
  circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(ptCenter.x - circleRadius, ptCenter.y - circleRadius, 2.0f * circleRadius, 2.0f * circleRadius) cornerRadius:circleRadius].CGPath;

  [circle setFillColor:[UIColor redColor].CGColor];
  [circle setStrokeColor:[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor];
  [circle setLineWidth:0.0f];

  [self.view.layer addSublayer:circle];
  //[self drawCircleWithRadius:circleRadius];
}

- (IBAction)onTapid)sender
{
  [self animateImageView];
}

- (void)animateImageView
{
   circleRadius += 70.0f;
   [self drawCircleWithRadius:circleRadius];
}

- (void)drawCircleWithRadiusCGFloat)radius 
{

  CGPoint ptCenter = self.view.center;
  CGRect rectBezierPath = CGRectMake(ptCenter.x - radius, ptCenter.y - radius, 2.0f * radius, 2.0f * radius);

  CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath"path"];

  [pathAnimation setFromValueid)circle.path];
  [pathAnimation setToValueid)[UIBezierPath bezierPathWithRoundedRect:rectBezierPath cornerRadius:circleRadius].CGPath];

  [pathAnimation setDuration:0.5f];
  [pathAnimation setRepeatCount:1.0f];

  [pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

  circle.path = [UIBezierPath bezierPathWithRoundedRect:rectBezierPath cornerRadius:circleRadius].CGPath;
  [circle addAnimation:pathAnimation forKey"changePathAnimation"];
}

关于ios - 从屏幕中心扩大一个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893336/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap