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

时间:2010-06-14 06:47:06

标签: iphone cocoa-touch rotation cgaffinetransform

我使用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];

2 个答案:

答案 0 :(得分:2)

这是一个系统错误: “Important如果视图的转换属性不包含标识转换,则该视图的框架是未定义的,其自动调整行为的结果也是如此。”

来自:Handling Layout Changes Automatically Using Autoresizing Rules

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

    parentView.autoresizesSubviews = NO;

答案 1 :(得分:0)

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