我知道这是通过网络编程完成的,它可以用xcode和obj-c吗?我有一个精灵表,有两个按钮实例(正常并单击)。我知道我可以使用obj-c中的click函数但是我可以导入一个精灵表并裁剪一部分精灵表(比如网站)来裁剪一张精灵表中所需的图像部分吗?如果是这样,需要什么功能,你有什么例子可以指出我吗?
答案 0 :(得分:0)
您可以使用CALayer
:它比UIImageView
重量轻,而且使用起来非常简单。这个想法是基于this post的例子,并且从工作项目中解脱出来。
// "sprites.png" is a 192x128 picture containing two rows of three 64x64 sprites each
NSString *path = [[NSBundle mainBundle] pathForResource:@"sprites.png" ofType:nil];
CGImageRef img = [UIImage imageWithContentsOfFile:path].CGImage;
imgLayer = [CALayer layer];
imgLayer.contents = (__bridge id)img;
// Frame defines the position of your sprite inside your view
imgLayer.frame = CGRectMake(horOffset, verOffset, 64, 64);
imgLayer.bounds = CGRectMake(0, 0, 64, 64); // The size of an individual sprite
// Pick the sprite from the top row, left column
imgLayer.contentsRect = CGRectMake(0, 0, 1.0/3.0, 1.0/2.0);
// Top row, middle column
// imgLayer.contentsRect = CGRectMake(1.0/3, 0, 1.0/3.0, 1.0/2.0);
// Top row, right column
// imgLayer.contentsRect = CGRectMake(2.0/3, 0, 1.0/3.0, 1.0/2.0);
// Bottom row, left column
// imgLayer.contentsRect = CGRectMake(0, 1.0/2, 1.0/3.0, 1.0/2.0);
// Bottom row, middle column
// imgLayer.contentsRect = CGRectMake(1.0/3, 1.0/2, 1.0/3.0, 1.0/2.0);
// Bottom row, right column
// imgLayer.contentsRect = CGRectMake(2.0/3, 1.0/2, 1.0/3.0, 1.0/2.0);
[self.layer addSublayer:imgLayer];