我正在尝试使用png图像在Xcode制作核心动画。 到目前为止,我使用CGRectMake绘制了一个小盒子,使盒子在加载时在屏幕上滑动。 现在,我将用layer.contents = [UIImage imageNamed:@""];替换CGRectlayer.contents = [UIImage imageNamed:@""]; 输入行。 有什么帮助吗? 谢谢。

(其余工作代码:))

。H

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewController : UIViewController

{
CAShapeLayer * layer;
UIImage * new; //hypothetical image

}

@end

.M

#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController




//[self view]

- (void)viewDidLoad
{   //layer
layer = [CAShapeLayer layer];
layer.position = CGPointMake ([self view].bounds.size.width /2, [self view].bounds.size.height    /2 );
layer.bounds= CGRectMake (0,0,100,100);
layer.borderWidth = 2;
layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:1 ] CGColor] ;


[[[self view] layer] addSublayer: layer];



CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
moveAnimation.duration = 1;
//starting point for the animation
moveAnimation.fromValue = [NSNumber numberWithFloat:layer.position.y];
//end point for the animation
moveAnimation.toValue = [NSNumber numberWithFloat:layer.position.y + 100];
//this line makes the animation stay to its position after moving
layer.position = CGPointMake(layer.position.x, layer.position.y +100);


[layer addAnimation:moveAnimation forKey: @"move1"];





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

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

添加行后终于让它工作了

UIImage *image2 = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]
                                                          pathForResource:@"pic@2x" ofType:@"png"]];

在图层上方,并指定建议的新娘演员表。 这是did load方法的最终代码。

- (void)viewDidLoad
{
UIImage *image2 = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]
                                                          pathForResource:@"pic@2x" ofType:@"png"]];
layer = [CALayer layer];
layer.contents = (__bridge id)image2.CGImage;
layer.position = CGPointMake ([self view].bounds.size.width /2, [self view].bounds.size.height /3 );
layer.bounds= CGRectMake (100,100,500,500);
[[[self view] layer] addSublayer: layer];


CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
moveAnimation.duration = 1;

moveAnimation.fromValue = [NSNumber numberWithFloat:layer.position.y];
moveAnimation.toValue = [NSNumber numberWithFloat:layer.position.y + 100];

layer.position = CGPointMake(layer.position.x, layer.position.y +100);


[layer addAnimation:moveAnimation forKey: @"move1"];



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

}

===============>>#1 票数:0 已采纳

您可以像这样将图层的内容设置为UIImage:

layer.contents = (__bridge id)image.CGImage;

一层的内容采用CGImageRef (核心图形图像参考)-这是为了允许OS X和iOS之间的兼容性。 因此,您需要传入UIImage对象的CGImage属性,该属性是上一行中的image OS X上的NSImage具有相同的属性。

但是,CGImageRef不是NSObject ,而是核心基础类型。 (__bridge id)是一个桥接 (__bridge id) ,您可以使用它来使编译器将其视为NSObject 您可以通过搜索桥接强制转换或免费电话桥接来了解有关这些原理的更多信息,但这不在此答案的范围内。

您可能应该对基本CALayer而不是形状图层执行此操作。 形状图层希望根据您指定的路径制作自己的内容。

  ask by user3233623 translate from so

未解决问题?本站智能推荐: