0

I subclassed a UIImageView. When it is double tapped, it will enlarge itself by 40%. I also have a UILabel that i place in the custom UIImageView and would like it to enlarge by 40%.

i can't seem to get it to animate correctly.

-(void)enlargeImage
{
    CGPoint pointCenter = self.center;
    CGPoint labelCenter = self.displayNameLabel.center;

    [UIView animateWithDuration:1
                          delay:0
                        options:(UIViewAnimationOptionAllowUserInteraction) 
                     animations:^{
                     self.frame = CGRectMake(self.frame.origin.x,
                                             self.frame.origin.y,
                                             self.frame.size.width * (1 + self.sizeMultiplier),
                                             self.frame.size.height * (1 + self.sizeMultiplier));
                         // Commented out after setting the autoresizeMask on the label
                         /*
                         self.displayNameLabel.frame = CGRectMake(self.displayNameLabel.frame.origin.x,
                                                                  self.displayNameLabel.frame.origin.y,
                                                                  self.displayNameLabel.frame.size.width * (1 + self.sizeMultiplier),
                                                                  self.displayNameLabel.frame.size.height * (1 + self.sizeMultiplier));
                         */
                         self.layer.shadowOpacity = 1;
                     }
                     completion:^(BOOL finished)
                     {
                         [self callDelegateSelect];
                     }];

    self.center = pointCenter;

    //self.displayNameLabel.center = labelCenter;
    //self.displayNameLabel.center = CGPointMake((self.frame.size.width / 2), (self.frame.size.height / 2));

    self.isSelected = TRUE;
}


-(void)shrinkImageWithZoomScale:(float)zoomScale
{
    CGPoint pointCenter = self.center;

    CGPoint labelCenter = self.displayNameLabel.center;

    [UIView animateWithDuration:1
                          delay:0
                        options:(UIViewAnimationOptionAllowUserInteraction)
                     animations:^{
                     self.frame = CGRectMake(self.frame.origin.x,
                                             self.frame.origin.y,
                                             (self.originalHeight / zoomScale),
                                             (self.originalHeight / zoomScale));

                         // Commented out after setting the autoresizeMask on the label
                         /*
                         self.displayNameLabel.frame = CGRectMake(self.displayNameLabel.frame.origin.x,
                                                                  self.displayNameLabel.frame.origin.y,
                                                                  ((self.originalLableHeight) / zoomScale),
                                                                  ((self.originalLableHeight) / zoomScale));
                         */
                         self.layer.shadowOpacity = 0;
                     }
                     completion:^(BOOL finished)
                    {
                        [self callDelegateDeselect];
                    }];

    self.center = pointCenter;
    //self.displayNameLabel.center = labelCenter;
    //self.displayNameLabel.center = CGPointMake((self.frame.size.width / 2), (self.frame.size.height / 2));

    self.isSelected = FALSE;
}

the custom UIImageView animates exactly how i want it too, but the UILabel misbehaves.

unselected (UILabel's background is set to orange):

unselected

selected:

selected

i'm guessing since the UIImageView's point reference is changing, its messing up the points i'm telling the UILabel to be?

EDIT:

the custom UIImageView's:

self.autoresizesSubviews = TRUE;

the subview UILabel:

self.displayNameLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

this solves the sizing issue, but it does not grow from the center; when growing, it shifts right and animates left to the center. when shrinking, it shifts left and animates right to the center. I also commented out setting the label's frame in the animation code.

4

1 回答 1

1

如果UILabel是 的子视图UIImageView,您可以尝试设置 的自动调整大小选项,UILabel以便在其父视图发生时自动调整大小。

此外,请确保将 的autoresizesSubviews属性UIImageView设置为YES(顺便说一下,这是默认设置)。

于 2012-09-26T15:17:02.857 回答