繁体   English   中英

动画UIView / UIImageView的裁剪

[英]Animating the cropping of UIView/ UIImageView

我将这段代码放在accelerometer: didAccelerate可以更改图像的裁剪。 基本上,当您倾斜iPad时,图像会被截断( 不调整大小 ),这正是我要的效果。 注意: self.xPos受accelerometer.x的位置影响。

UIImage *originalStringImage = [UIImage imageNamed:@"string.png"];
CGImageRef imageRef = CGImageCreateWithImageInRect([originalStringImage CGImage], CGRectMake(0.0f,0.0f, self.xPos+50.0f, 26.0f));
[self.stringImageView setImage:[UIImage imageWithCGImage:imageRef]];
self.stringImageView.frame = CGRectMake(10.0f, self.stringYPos, self.xPos+50.0f, 26.0f);

现在,我想做完全一样的事情,尽管作为UIView动画。 这些UIView动画参数之间的区别:

UIImageView *pageStringImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, placeString, 0.0f, 42.0f)];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDuration:3.0];
pageStringImageView.frame = CGRectMake(10.0f, placeString, 900.0f, 42.0f);
UIImage *originalStringImage = [UIImage imageNamed:@"string.png"];
CGImageRef imageRef = CGImageCreateWithImageInRect([originalStringImage CGImage], CGRectMake(10.0f,placeString, pageStringImageView.frame.origin.x, 42.0f));
[pageStringImageView setImage:[UIImage imageWithCGImage:imageRef]];
[UIView commitAnimations]; 

但是上述方法并不能解决所有问题。 实际上,它甚至不显示图像。 我相信这里的主要困难是在运行UIView动画时动态更改图像的大小。

有什么建议或问题吗?

如果您使用UIImageView的缩放属性( .contentMode属性),则此操作应该非常简单。

视图(包括图像视图)可以多种方式显示该内容。 图像视图的默认设置是缩放内容,因此,当您更改图像的帧/​​边界时,它会适当缩小/扩展。

因此,如果要使用标准的UIView动画块来调整UIImageView的框架,则它将使图像缩放或缩小动画。 但是,如果您更改图像视图的.contentMode属性(如果更改框架),则可以使其裁剪。

例如, UIViewContentModeLeft将使内容与视图的左侧对齐,因此,如果您调整视图的宽度,它将从右侧“裁剪”。 查看UIView文档以查看contentMode可以拥有的所有值。 因此,基本上,只需为视图的帧设置动画,但可以适当地设置contentMode即可获得所需的裁剪效果。 您可能需要确保将视图设置为裁剪到边界,尽管我感觉图像视图默认情况下会这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM