在iPhone中dynamic重新绘制大小的圆

我试图做的是让用户触摸屏幕,当触摸被检测到的位置,圆的意图是增长的规模(并保持增长),直到用户放手。

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

做这个的最好方式是什么?

在这里输入图像说明

我将使用UILongPressGestureRecognizerNSTimerUIBezierPath的组合来实现这一点。

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

 - (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]; } 

然后,当长按手势被识别时,检查其状态。 如果状态开始,则启动一个重复定时器来调用animationfunction。 当手势结束时,无效。

 - (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]; } 

最后,下面的这个函数将使用一个CABasicAnimationanimation形状图层的path属性为上面指出的值(每次+10)。 确保这个animation持续时间不高于定时器重复间隔!

 - (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"]; } 

希望这可以帮助!

最好的办法是使用Quartz 2D并在触摸时为graphics创buildanimation。

使用这样的东西..

在这里输入图像说明

 CGContextRef ctxt = [[NSGraphicsContext currentContext] graphicsPort]; CGGradientRef gradient; CGColorSpaceRef colorSpace; CGFloat locations[] = {0.0,1.0}; CGFloat components[] = { 0.5,1.0,1.0,1.0, 0.25,0.5,0.5,1.0 }; colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); gradient = CGGradientCreateWithColorComponents(colorSpace,components,locations, sizeof(locations)/sizeof(CGFloat)); CGPoint start = {70.0,130.0}, end = {100.0,100.0}; CGFloat startRadius = 0.0, endRadius = 90.0; CGContextDrawRadialGradient(ctxt,gradient,start,startRadius,end,endRadius,0); CGGradientRelease(gradient); CGColorSpaceRelease(colorSpace); 

在这里find更多的信息。 在iOS开发中,使用Core Graphics和/或Quartz 2D,我怎样才能画出一个圆形,并且看起来像一个球体?

另外对于Quartz2d的animation技术,请看这里.. https://github.com/neror/CA360

您必须自定义UIView以在drawRect:方法中绘制圆。 处理你的viewController视图的捏效应,更新UIView自定义的variables,并使用:

 [myCicleView setNeedsDisplay]; 

强制视图重绘。

你可以试试这个:

触摸方法

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(reDrawCircle) userInfo:nil repeats:YES]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [myTimer invalidate]; myTimer = nil; } 

为了缩小图片::

 -(void)reDrawCircle { //Your Code for Circle Re-Draw. } 

当用户触摸时,它会启动定时器为你的方法重画圆,当用户结束触摸,它会使计时器无效。 所以它不会调用重绘的方法。

希望它能帮助你。
谢谢。

Interesting Posts