I started studying objective-c using the iPhone and iPad apps for Absolute Beginners by Rory Lewis book but i got stuck on the 5th chapter.
我开始使用Rory Lewis书中的Absolute Beginners的iPhone和iPad应用程序开始研究objective-c,但是我被困在第5章。
I want to make a button that shrinks an image.
My problem is, that after I wrote all the code, the image shrinks to the point (0, 0) of the UIImageView (the top left), while in the video the image shrinks to its center. I've done some research and found out that CGAffineTransform
uses the center of the object to make translations, rotations etc.
我想制作一个缩小图像的按钮。我的问题是,在我写完所有代码后,图像缩小到UIImageView(左上角)的点(0,0),而在视频中,图像缩小到其中心。我做了一些研究,发现CGAffineTransform使用对象的中心进行翻译,旋转等。
So why does it not work in my case?
那为什么它在我的情况下不起作用?
I have the latest Xcode version and haven't done anything strange. I hope I've been clear. Sorry for my English.
我有最新的Xcode版本并没有做任何奇怪的事情。我希望我已经清楚了。对不起我的英语不好。
EDIT Sorry for the code, but i wrote the question from my phone. Anyway the incriminated part goes something like this
编辑对不起代码,但我从手机上写了这个问题。无论如何,有罪的部分都是这样的
- (IBAction)shrink:(id)sender {
if(hasShrink == NO){
[shrinkButton setTitle:@"Grow" forState:UIControlStateNormal];
}
else if(hasShrink == YES){
[shrinkButton setTitle:@"Shrink" forState:UIControlStateNormal];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
myIcon.transform = CGAffineTransformMakeScale(.25, .25);
hasShrink = YES;
[UIView commitAnimations];
}
All the animations are correctly written and the app does work, it just shrinks to the top left. Why is the point not set to the center of the UIImageView by default like it should be?
所有的动画都是正确编写的,应用程序确实有效,它只是缩小到左上角。为什么默认情况下该点未设置为UIImageView的中心?
EDIT: I figured out it was a problem caused by AutoLayout. Once disabled everything went smooth ;)
编辑:我发现这是由AutoLayout引起的问题。一旦禁用,一切顺利;)
3 个解决方案
#1
9
If you are transforming a view
mangled
managed by auto-layout, you may experience some strange side-effects. See this answer for more details.
如果要转换由自动布局管理的视图,您可能会遇到一些奇怪的副作用。有关详细信息,请参阅此答案。
The solution I ended up employing was to encapsulate the view I wanted to transform inside another view of exactly the same size. The outer view had the appropriate layout constraints applied to it while the inner view was simply centered in its container. Applying a CGAffineTransform
scale transform to the inner view now centers properly.
我最终采用的解决方案是封装我想在另一个完全相同大小的视图内转换的视图。外部视图应用了适当的布局约束,而内部视图只是在其容器中居中。现在,将CGAffineTransform比例变换应用于内部视图是正确的。
#2
8
Old question... but just in case others come looking:
老问题......但是以防万一其他人来看:
The CGAffineTransform
acts (rotates, scales) around an anchorPoint.
CGAffineTransform围绕anchorPoint行动(旋转,缩放)。
If you are sizing to 0, 0 then your anchor point is set to 0, 0. If you want it to size to a different point, such as the center of the image, you need to change the anchor point.
如果要调整为0,0,则将锚点设置为0,0。如果要将其大小设置为不同的点(例如图像的中心),则需要更改锚点。
The anchor is part of a layer
锚是图层的一部分
So if you have a UIImageView
called imageview
, you would make a call like this to set the anchor to the center of imageview:
因此,如果您有一个名为imageview的UIImageView,您可以像这样调用将锚点设置为imageview的中心:
[imageview.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
#3
7
Try something like this:
尝试这样的事情:
CGAffineTransform t = CGAffineTransformMakeScale(0.5, 0.5);
CGPoint center = imageView.center; // or any point you want
[UIView animateWithDuration:0.5
animations:^{
imageView.transform = t;
imageView.center = center;
}
completion:^(BOOL finished) {
/* do something next */
}];
Next time show your code. It will be easier to help you.
下次显示您的代码。它会更容易帮助你。
Check this project: https://github.com/djromero/*/archive/Q-13706255.zip
检查这个项目:https://github.com/djromero/*/archive/Q-13706255.zip
You must study how autolayout and autoresize affect your views. In the project above both are disabled to let you see how it works.
您必须研究autolayout和autoresize如何影响您的视图。在上面的项目中,两个都被禁用,让你看看它是如何工作的。
#1
9
If you are transforming a view
mangled
managed by auto-layout, you may experience some strange side-effects. See this answer for more details.
如果要转换由自动布局管理的视图,您可能会遇到一些奇怪的副作用。有关详细信息,请参阅此答案。
The solution I ended up employing was to encapsulate the view I wanted to transform inside another view of exactly the same size. The outer view had the appropriate layout constraints applied to it while the inner view was simply centered in its container. Applying a CGAffineTransform
scale transform to the inner view now centers properly.
我最终采用的解决方案是封装我想在另一个完全相同大小的视图内转换的视图。外部视图应用了适当的布局约束,而内部视图只是在其容器中居中。现在,将CGAffineTransform比例变换应用于内部视图是正确的。
#2
8
Old question... but just in case others come looking:
老问题......但是以防万一其他人来看:
The CGAffineTransform
acts (rotates, scales) around an anchorPoint.
CGAffineTransform围绕anchorPoint行动(旋转,缩放)。
If you are sizing to 0, 0 then your anchor point is set to 0, 0. If you want it to size to a different point, such as the center of the image, you need to change the anchor point.
如果要调整为0,0,则将锚点设置为0,0。如果要将其大小设置为不同的点(例如图像的中心),则需要更改锚点。
The anchor is part of a layer
锚是图层的一部分
So if you have a UIImageView
called imageview
, you would make a call like this to set the anchor to the center of imageview:
因此,如果您有一个名为imageview的UIImageView,您可以像这样调用将锚点设置为imageview的中心:
[imageview.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
#3
7
Try something like this:
尝试这样的事情:
CGAffineTransform t = CGAffineTransformMakeScale(0.5, 0.5);
CGPoint center = imageView.center; // or any point you want
[UIView animateWithDuration:0.5
animations:^{
imageView.transform = t;
imageView.center = center;
}
completion:^(BOOL finished) {
/* do something next */
}];
Next time show your code. It will be easier to help you.
下次显示您的代码。它会更容易帮助你。
Check this project: https://github.com/djromero/*/archive/Q-13706255.zip
检查这个项目:https://github.com/djromero/*/archive/Q-13706255.zip
You must study how autolayout and autoresize affect your views. In the project above both are disabled to let you see how it works.
您必须研究autolayout和autoresize如何影响您的视图。在上面的项目中,两个都被禁用,让你看看它是如何工作的。