ios - iPhone : Animate circle with UIKit

标签 ios objective-c uikit

我正在使用一个CircleView类,它基本上继承了UIView并实现了drawRect来绘制一个圆圈。这一切都有效,万岁!

但我不知道如何制作它,以便当我触摸它(实现触摸代码)时,圆圈会变大或弹出。通常我会使用 UIKit 动画框架来执行此操作,但考虑到我基本上是重写 drawRect 函数来直接绘制圆圈。那么我该如何制作动画呢?

- (void)drawRect:(CGRect)rect{
    CGContextRef context= UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, _Color.CGColor);
    CGContextFillEllipseInRect(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    // Animate?
}

最佳答案

答案取决于您所说的“生长或流行”的含义。当我听到“pop”时,我认为 View 会在短时间内放大,然后再次缩小到原始大小。另一方面,“增长”的东西会扩大但不会再次缩小。

对于在短时间内再次缩放的东西,我会使用变换来缩放它。无论是否自定义绘图,UIView 都内置了对简单变换动画的支持。如果这就是您正在寻找的内容,那么只需几行代码即可。

[UIView animateWithDuration:0.3
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse // reverse back to original value
                 animations:^{
                     // scale up 10%
                     yourCircleView.transform = CGAffineTransformMakeScale(1.1, 1.1);
                 } completion:^(BOOL finished) {
                     // restore the non-scaled state
                     yourCircleView.transform = CGAffineTransformIdentity;
                 }];

另一方面,如果您希望每次点击时圆圈都变大一点,那么这对您来说就不行了,因为 View 在放大时看起来会像素化。制作自定义动画可能很棘手,因此我仍然建议您对实际动画使用缩放变换,然后在动画后重新绘制 View 。

[UIView animateWithDuration:0.3
                 animations:^{
                     // scale up 10%
                     yourCircleView.transform = CGAffineTransformMakeScale(1.1, 1.1);
                 } completion:^(BOOL finished) {
                     // restore the non-scaled state
                     yourCircleView.transform = CGAffineTransformIdentity;
                     // redraw with new value
                     yourCircleView.radius = theBiggerRadius;
                 }];

如果您真的非常想要制作完全自定义的动画,那么我建议您观看 Rob Napiers talk on Animating Custom Layer Properties ,他的例子甚至就是你正在做的事情(画一个圆圈)。

关于ios - iPhone : Animate circle with UIKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19668824/

相关文章:

iphone - MKMapView错误

objective-c - 如何与 Mac OS X 中的 Photoshop 应用程序交互

ios - 当我不拥有委托(delegate)时,如何检测与 UIPickerView 的交互

ios - 多次调用 glDrawElements 是否比在 GLSL 中对每个片段进行相同的计算更有效?

ios - 选中单元格后如何移动

ios - UIView 周围的虚线边框

css - 将 2 个内联列垂直放置到背景中间

ios - SecItemCopyMatching 返回 -25300

iphone - 显示图像和字符串的 UIPIckerView

ios - 在曲线中移动 UIImageview