在iPhone中动态重绘重新调整大小的圆圈 [英] Dynamically redraw re sized circle in iPhone

查看:128
本文介绍了在iPhone中动态重绘重新调整大小的圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是让用户触摸屏幕,当在圆圈意图增大尺寸(并保持增长)的位置检测到触摸时,直到用户放开为止。

What im trying to do is have a user touch the screen, when the touch is detected at the position the circle is meant to grow in size (and keep growing) until the user lets go.

我知道如何检测触摸但是我正在尝试绘制的问题并且随着它变大而重新绘制圆圈。

I know how to detect touches but the problem im having is trying to draw and have the circle re drawn as its getting larger.

什么这是最好的方法吗?

What is the best way to do this?

推荐答案

我会使用 UILongPressGestureRecognizer 的组合, NSTimer UIBezierPath 实现此目的。

I would use a combination of UILongPressGestureRecognizer, NSTimer, and UIBezierPath to achieve this.

首先,设置 viewDidLoad 以添加和配置将容纳您想要绘制的圆圈的CAShapeLayer。以下代码最初将在半径为50磅的屏幕上绘制一个圆,并在主视图中添加长按手势。当然,触摸检测可以按照您想要的方式完成。哦,你需要为此导入Quartz。

First, set up your viewDidLoad to add and configure the CAShapeLayer that will house the circle you wish to draw. The following code will initially draw a circle on screen with a radius of 50 points, and add a long press gesture to the main view. Of course the touch detection can be done in what ever way you'd like. Oh, and you'll need to import Quartz for this.

- (void)viewDidLoad
{
    [super viewDidLoad];

    circleRadius = 50.0f;

    circle = [CAShapeLayer layer];

    [circle setAnchorPoint:CGPointMake(0.5f, 0.5f)];
    [circle setFillColor:[UIColor clearColor].CGColor];
    [circle setStrokeColor:[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor];
    [circle setLineWidth:5.0f];

    [self.view.layer addSublayer:circle];

    [self drawCircleWithRadius:circleRadius];

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)];
    [longPress setMinimumPressDuration:0.1];
    [longPress setAllowableMovement:15.0f];
    [self.view addGestureRecognizer:longPress];
}

然后,当识别出长按手势时,检查其状态。如果其状态为开始,则启动重复计时器以调用动画功能。当手势结束时,无效。

Then, when the long press gesture is recognized, check its state. If its state is began start a repeating timer to call the animation function. When the gesture ends, invalidate.

- (void)longPressDetected:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animateImageView) userInfo:nil repeats:YES];
    }else if (sender.state == UIGestureRecognizerStateEnded) {
        [timer invalidate];
    }
}

- (void)animateImageView {
    circleRadius += 10.0f;
    [self drawCircleWithRadius:circleRadius];
}

最后,下面这个函数将使用CABasicAnimation动画显示路径属性形状层到上面指出的值(每次+10)。确保此动画持续时间不高于定时器重复间隔!

And finally, this function below will use a CABasicAnimation animate the path property of the shape layer to the value indicated above (+10 every time). Make sure that this animations duration isn't higher than the timers repeats interval!

- (void)drawCircleWithRadius:(CGFloat)radius {
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    [pathAnimation setFromValue:(id)circle.path];
    [pathAnimation setToValue:(id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0f * radius, 2.0f * radius) cornerRadius:radius].CGPath];
    [pathAnimation setDuration:0.1];
    [pathAnimation setRepeatCount:1.0f];
    [pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath;
    circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius, CGRectGetMidY(self.view.frame)-radius);
    [circle addAnimation:pathAnimation forKey:@"changePathAnimation"];
}

希望这会有所帮助!

这篇关于在iPhone中动态重绘重新调整大小的圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆