简体   繁体   中英

scaling a UIImageView at the center of a UIView

I'm trying to make a scaling ring, which scales in the center of a UIView.

Setup is the following:

In IB, I have a view and my ImageView attached to it (just a transparent PNG with a ring). Setup an IBOutlet to the ImageView (theRing) and a button to trigger the action.

ImageView is set to «scaleToFill» in IB.

The code for the action is:

-(IBAction)buttonPressed:(id)sender{

    CGRect theFrame = theRing.frame;

    theFrame.size.width = 16;
    theFrame.size.height = 16;

    [theRing setFrame:theFrame];  //Scale down the ring first
    theRing.center = [self.view center];  // Center the ring in the center of the view

    theFrame.size.width = 300;
    theFrame.size.height = 300;

    [theRing setAlpha:1.0];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];

    [theRing setFrame: theFrame];
    [theRing setAlpha:0.0];
    theRing.center = [self.view center];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector: @selector(animEnd:finished:context:)];
    [UIView commitAnimations];

}

Now - when I hit the button the first time, the image is scaled with the reference point being the upper left corner of the ImageView (means, it scales, but the ring expands to the lower right of the screen).

BUT: When I hit the button again, the whole thing works as expected (means, the ring stays in the middle of the screen and is scaled).

Any ideas?

尝试使用ImageView模式的纵横比填充

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM