【问题标题】:Draw a gesture control resizable circle in iOS在 iOS 中绘制一个手势控制可调整大小的圆圈
【发布时间】:2014-11-26 12:23:32
【问题描述】:

我想在 iOS 中画一个圆圈。它可以是图像视图或视图,我想通过手势调整其大小。就像,从外面拖到中心,圆的半径应该减小,反之亦然。

请注意,我想固定该视图、圆圈或图像的位置。我只想在拖动时调整大小。

【问题讨论】:

  • 您想要 1) 一种显示圆的方法和 2) 一种控制其半径的方法。我会选择CAShapeLayer + UIPanGestureRecognizer
  • 嗨 Cyrille,你能帮我获取它的代码吗?
  • 我(以及这​​里的许多其他人)不会为您编写代码。你必须先做一些研究,按照我们给你的方向尝试,然后回答关于为什么特定代码行不起作用的精确问题。首先,您必须尝试使用​​平移手势识别器调整简单的正方形 UIView 的大小。
  • 亲爱的 Cryille,我知道这些课程可以完成,但我不知道该怎么做。所以我问这个门户是为了这个目的。

标签: ios objective-c iphone cgrect


【解决方案1】:

试试这个

  - (IBAction)pinch:(UIPinchGestureRecognizer *)sender {

    double scale=[sender scale];
  self.view1.frame=CGRectMake(self.view1.frame.origin.x*scale,     self.view1.frame.origin.y*scale, self.view1.frame.size.width*scale, self.view1.frame.size.height*scale);

[self.view1.layer setCornerRadius:5];

}

【讨论】:

    【解决方案2】:

    试试这个。

    - (IBAction)pan:(UIPanGestureRecognizer *)sender {
        for (CALayer *layer in self.imageView.layer.sublayers) {
        [layer removeFromSuperlayer];
        }
        CGPoint scale =[sender locationInView:sender.view];
    
        CAShapeLayer *circleLayer = [CAShapeLayer layer];
    
        [circleLayer setBounds:CGRectMake(0.0f, 0.0f, [_imageView bounds].size.width,
                                      [_imageView bounds].size.height)];
        [circleLayer setPosition:CGPointMake(CGRectGetMidX([_imageView bounds]),CGRectGetMidY([_imageView bounds]))];
    
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, CGRectGetWidth(_imageView.frame)+scale.x, CGRectGetHeight(_imageView.frame)+scale.y)];
    
        [circleLayer setPath:[path CGPath]];
    
        [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
    
        [circleLayer setLineWidth:1.0f];
        [circleLayer setFillColor:[[UIColor clearColor] CGColor]];
    
    
        [[_imageView layer] addSublayer:circleLayer];
    
       }
    

    【讨论】:

    • 谢谢 Johny,但如果你能告诉我如何在 UIPanGestureRecognizer 上实现它而不是捏,那就更好了。此外,我们是否可以设置它的边界,使其最大尺寸不会越过视图?
    • 每个捏回调上重新创建一个层?这是对资源的巨大浪费。
    • 我只是告诉他该做什么,他可以在其他地方继续初始化。
    • Johny 的想法适用于我的东西。如果您知道 Cyrille 的任何其他方式,请教我如何像 Johny 那样做到这一点,而不是提供建议。谢谢。
    • 感谢您的赞赏。
    猜你喜欢
    • 2014-06-10
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多