我将UIImageView子类化。当它被双击时,它会自动扩大40%。我也有一个UILabel,我放在自定义UIImageView中,并希望它放大40%。
我似乎无法正确地制作动画。
-(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;
}
自定义UIImageView也是我想要的动画,但是UILabel行为不端。
未选中(UILabel的背景设置为橙色):
选自:
我猜是因为UIImageView的点引用正在改变,它弄乱了我告诉UILabel的点?
编辑:
自定义UIImageView:
self.autoresizesSubviews = TRUE;
子视图UILabel:
self.displayNameLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
这解决了尺寸问题,但它并没有从中心发展;在成长时,它会向右移动,并向左移动到中心。收缩时,它向左移动并向右移动到中心。我还注释了在动画代码中设置标签的框架。
答案 0 :(得分:1)
如果UILabel
是UIImageView
的子视图,您可以尝试设置UILabel
的自动调整选项,以便在超级视图执行时自动调整大小。
另外,请确保autoresizesSubviews
的{{1}}属性设置为UIImageView
(顺便说一下,这是默认设置)。