CGAffineTransform and scaling to center of the image

后端 未结 3 898
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 02:16

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

相关标签:
3条回答
  • 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/StackOverflow/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.

    0 讨论(0)
  • 2021-01-12 02:50

    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.

    0 讨论(0)
  • 2021-01-12 02:57

    Old question... but just in case others come looking:

    The CGAffineTransform acts (rotates, scales) around an 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.

    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.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
    
    0 讨论(0)
提交回复
热议问题