我正在舍入一个UIBUtton,这很好(自我是一个uibutton子类):
self.layer.cornerRadius = self.frame.size.width/2; self.layer.masksToBounds = YES; self.clipsToBounds = YES;
但我也试图动画按钮缩小比例然后返回到原始大小,如下所示:
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f); } completion:^(BOOL finished) { [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f); } completion:nil]; }];
收缩/恢复动画应该发生.然而,之后我失去了角落半径,按钮又回到了正方形.如何在保持圆形按钮的同时动画和更改变换比例?
我也试过以下.它会在动画结束时将按钮恢复为圆形状态,但是在动画开始时它会回到正方形并且它看起来非常糟糕:
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f); self.layer.cornerRadius = 0.0f; self.layer.masksToBounds = YES; self.clipsToBounds = YES; } completion:^(BOOL finished) { [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f); self.layer.cornerRadius = self.frame.size.width/2; self.layer.masksToBounds = YES; self.clipsToBounds = YES; } completion:nil]; }];
编辑:我需要动画代码和舍入按钮代码,所有发生在子类按钮.当触摸按钮时,它需要在按钮类内部发生(即,没有用于舍入按钮或调用动画的代码可以在视图控制器中).
用澄清的解决方案编辑:我不知道为什么会这样,但以下建议的一部分似乎解决了这个问题.我将动画方法添加为UIControlEventTouchDown的自定义按钮上的动作.我删除了控件事件操作,而是从子类化按钮类中的 - (void)touchesBegan ....调用动画方法,一切似乎都正常.不知道为什么会这样.如果有人知道为什么会这样,那么会喜欢在评论或其他内容中进一步澄清/解释.再次感谢@Shan的帮助