UIView/ UIImageView 的裁剪动画

发布于 2024-12-10 15:32:46 字数 1358 浏览 0 评论 0原文

我有这段代码放置在 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 动画运行时动态更改图像的大小。

有什么建议或问题吗?

I have this chunk of code which is placed in accelerometer: didAccelerate which changes the cropping of an image. Basically as you tilt an iPad an image gets truncated (not resized) which is exactly the effect I'm looking for. Note: self.xPos is effected by the position of 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);

Now I would like to do the exact same thing, although as a UIView animation. Something between these UIView animation parameters:

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

But the above doesn't work it all. In fact it doesn't even show the image. I believe the main difficulty here is changing the size of the image dynamically while the UIView animation is running.

Any suggestions or questions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2024-12-17 15:32:46

如果您使用 UIImageView 的缩放属性(.contentMode 属性),这应该相当简单。

视图(包括图像视图)可以通过多种方式显示内容。图像视图的默认设置是缩放内容,因此当您更改图像的框架/边界时,它会相应地缩小/扩展。

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

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

This should be fairly simple to do if you use the scaling properties of your UIImageView (the .contentMode property).

Views - including image views - can display there content in a number of ways. The default for an image view is to scale the content, so when you change the frame/bounds of the image it shrinks / expands as appropriate.

So if you were to use a standard UIView animation block to adjust the frame of a UIImageView it would animate the image scaling or shrinking. But if you change the image view's .contentMode property if you change the frame you can have it crop instead.

For example, UIViewContentModeLeft will align the content with the left hand side of the view, so if you adjust the width of the view it will be 'cropped' from the right side. Check the UIView documentation to see all the values you can have for contentMode. So basically, just animate your view's frame changing, but set the contentMode appropriately to give you the desired cropping effect. You may need to make sure your view is set to clip to bounds, although I have a feeling image views do this by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文