【问题标题】:mak a uiimageView turn around another image制作 uiimageView 转另一张图片
【发布时间】:2011-09-21 08:12:59
【问题描述】:

这是我的问题。 我想为一个图像制作动画,我想让它围绕另一个图像旋转,但我不知道该怎么做。我只想让它转动(不是旋转,而是像绕圈一样转动的运动)。请问我该怎么做?


这是我的代码:

-(void){
CALayer *orbit1 = [CALayer layer];
orbit1.bounds = CGRectMake(0, 0, 200, 200);
orbit1.position = centre.center;
orbit1.cornerRadius = 100;
orbit1.borderColor= [UIColor redColor].CGColor;
orbit1.borderWidth = 1.5;

planet1 = [CALayer layer];
planet1.bounds = CGRectMake(0, 0, 44.0, 20.0);
planet1.position = CGPointMake(160, 25);
planet1.contents = (id)([UIImage imageNamed:@"bouletest_05.png"].CGImage);
[orbit1 addSublayer:planet1];

CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim1.fromValue = [NSNumber numberWithFloat:0];
anim1.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
anim1.repeatCount = HUGE_VALF;
anim1.duration = 10.0;
[orbit1 addAnimation:anim1 forKey:@"transform"];

[self.view.layer addSublayer:orbit1];

}

然后:

-(void)creation{

imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"abouffer_03.png"]];
[[self view] addSubview:imageView2];
imageView2.center=planet1.center;
}

看最后一行:imageView2.center=planet1.center;问题来了

【问题讨论】:

标签: iphone objective-c xcode animation uiimageview


【解决方案1】:

查看此帖子:http://nachbaur.com/blog/core-animation-part-4。在那里,您将找到一个如何沿任意 UIBezierPath 为视图设置动画的示例。

因此,在您的情况下,您需要围绕“锚”图像创建一个圆形路径:

const CGFloat radius = 100.0f;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(center.x-radius, center.y-radius, 2*radius, 2*radius)];

并将带有该路径的 CAKeyFrameAnimation 添加到要移动的图像视图。

【讨论】:

  • 抱歉,当我想将带有该路径的 CAKeyFrameAnimation 添加到图像视图时,我遇到了一个小问题,我不知道该怎么做,请你帮帮我
  • @Valentin,关注链接的博客文章 - 它展示了如何添加动画。您对那里的描述有什么具体问题吗?
  • 是的,我从不使用 bezierPAth 等...当我在 xcode -(void) {then your code} 上书写时,它不起作用,屏幕上什么也没有发生
  • 对不起,我现在明白了。但现在我有另一个问题我想使用正在转动的图像,更准确地说是它的中心将其分配给其他图像但是我使用中心 xcode 告诉我请求成员“中心”不是结构或联合跨度>
  • @valentingougeau 你到底想怎么做? (邮政编码可能)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多