CGAffineTransform:UIButton 上的旋转调整按钮图像的大小

发布于 2024-09-05 17:31:38 字数 1099 浏览 5 评论 0原文

我在使用 UIButtons 时遇到了一个奇怪的问题,输入 custom。我将其中 4 个按钮放置在 UIScrollview 上,并使用 CGAffineTransform 将每个按钮旋转随机角度。现在看来按钮本身会根据旋转角度改变大小。

UIGraphicsBeginImageContext(tempCtxSize);
[cookbookImage drawInRect:CGRectMake(imgOffsetX, imgOffsetY+frmOffsetY, cookbookImage.size.width, cookbookImage.size.height)];
[cookbookFrame drawInRect:CGRectMake(0.0f, frmOffsetY, cookbookFrame.size.width, cookbookFrame.size.height)];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

…

UIButton *cookbookViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cookbookViewButton setFrame:CGRectMake(0.0f, 0.0f, combinedImage.size.width, combinedImage.size.height)];

[cookbookViewButton setBackgroundColor:[UIColor clearColor]];
[cookbookViewButton setBackgroundImage:combinedImage forState:UIControlStateNormal];

CGAffineTransform rotation = [cookbookViewButton transform];
rotation = CGAffineTransformRotate(rotation, angle); // some random angle
[cookbookViewButton setTransform:rotation];

I've got a strange problem using UIButtons, type custom. I'm placing 4 of those buttons on a UIScrollview, rotating each button by a random angle using CGAffineTransform. Now it seems that the buttons themselves change size depending on the angle of rotation.

UIGraphicsBeginImageContext(tempCtxSize);
[cookbookImage drawInRect:CGRectMake(imgOffsetX, imgOffsetY+frmOffsetY, cookbookImage.size.width, cookbookImage.size.height)];
[cookbookFrame drawInRect:CGRectMake(0.0f, frmOffsetY, cookbookFrame.size.width, cookbookFrame.size.height)];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

…

UIButton *cookbookViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cookbookViewButton setFrame:CGRectMake(0.0f, 0.0f, combinedImage.size.width, combinedImage.size.height)];

[cookbookViewButton setBackgroundColor:[UIColor clearColor]];
[cookbookViewButton setBackgroundImage:combinedImage forState:UIControlStateNormal];

CGAffineTransform rotation = [cookbookViewButton transform];
rotation = CGAffineTransformRotate(rotation, angle); // some random angle
[cookbookViewButton setTransform:rotation];

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

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

发布评论

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

评论(2

温馨耳语 2024-09-12 17:31:38

这是一个系统错误:
重要 如果视图的变换属性不包含恒等变换,则该视图的框架是未定义的,其自动调整大小行为的结果也是如此。”

来自:使用自动调整大小规则自动处理布局更改< /a>

解决方案:将父视图的autoResizeSubviews设置为NO。

    parentView.autoresizesSubviews = NO;

This is a system bug:
" Important If a view’s transform property does not contain the identity transform, the frame of that view is undefined and so are the results of its autoresizing behaviors."

from: Handling Layout Changes Automatically Using Autoresizing Rules

Solution: set the parent view's autoResizeSubviews to NO.

    parentView.autoresizesSubviews = NO;
不交电费瞎发啥光 2024-09-12 17:31:38

不要设置 CookbookViewButton 的框架 - 设置边界。

Don't set the frame of cookbookViewButton - set the bounds.

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