CGAffine转换并缩放到图像的中心 [英] CGAffineTransform and scaling to center of the image

查看:109
本文介绍了CGAffine转换并缩放到图像的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 Rory Lewis为Absolute Beginners提供的iPhone和iPad应用程序来开始研究objective-c a>书但是我在第5章陷入困境。



我想制作一个缩小图像的按钮。

我的问题是,在我写完所有代码后,图像缩小到UIImageView(左上角)的点(0,0),而在视频中,图像缩小到其中心。我做了一些研究,发现 CGAffineTransform 使用对象的中心进行翻译,旋转等。



<那么为什么它在我的情况下不起作用?



我有最新的Xcode版本并没有做任何奇怪的事情。
我希望我已经清楚了。对不起,我的英文。



编辑
对不起代码,但我从手机上写了这个问题。
无论如何,被控制的部分都是这样的

   - (IBAction)shrink:(id)sender {

if(hasShrink == NO){
[shrinkBut​​ton setTitle:@GrowforState:UIControlStateNormal];
}
else if(hasShrink == YES){
[shrinkBut​​ton setTitle:@ShrinkforState:UIControlStateNormal];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
myIcon.transform = CGAffineTransformMakeScale(.25,.25);
hasShrink = YES;
[UIView commitAnimations];

}



所有动画都正确写的,应用程序确实有效,它只是缩小到左上角。为什么默认情况下该点没有设置为UIImageView的中心?应该是这样的?



编辑:
我想通了这是由AutoLayout引起的问题。
一旦禁用,一切顺利;)

解决方案

如果您正在转换视图 mangled 通过自动布局管理,您可能会遇到一些奇怪的副作用。 有关详细信息,请参阅此答案。



我结束的解决方案使用是封装我想在另一个完全相同大小的视图内转换的视图。外部视图应用了适当的布局约束,而内部视图只是在其容器中居中。将 CGAffineTransform 比例变换应用到内部视图现在可以正确居中。


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.

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.

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.

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?

EDIT: I figured out it was a problem caused by AutoLayout. Once disabled everything went smooth ;)

解决方案

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.

这篇关于CGAffine转换并缩放到图像的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆