使用UIImage进行CoreAnimation

时间:2014-04-21 05:46:40

标签: ios objective-c

我正在尝试使用png图像在Xcode中制作核心动画。到目前为止,我使用CGRectMake使用CGRect框在屏幕上滑动以绘制一个小框,它可以工作。现在我要用layer.contents = [UIImage imageNamed:@""];类型的行替换#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface ViewController : UIViewController { CAShapeLayer * layer; UIImage * new; //hypothetical image } @end 行。有帮助吗?感谢。

(其余工作代码:))

·H

#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.

的.m

- (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"]];

最后在添加行

后开始工作
- (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.

在图层正上方以及指定建议的新娘演员阵容。这是do load方法的最终代码。

{{1}}

}

1 个答案:

答案 0 :(得分:0)

您可以将图层内容设置为UIImage,如下所示:

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

图层为其内容采用CGImageRef(核心图形图像引用) - 这是为了允许OS X和iOS之间的兼容性。因此,您希望传递CGImage对象的UIImage属性,该对象在上面的行中为image。 OS X上的NSImage具有相同的属性。

但是,CGImageRef不是NSObject,而是核心基础类型。 (__bridge id)桥接广告,您可以使用它来使编译器将其视为NSObject。您可以通过搜索桥接演员或免费桥接来阅读有关这些原则的更多信息,但这不在本答案的范围之内。

您可能应该对基础CALayer而不是形状图层执行此操作。形状图层希望从您给出的路径中创建自己的内容。