如何在UIView中剪切路径?

时间:2016-01-02 05:37:50

标签: ios objective-c uiview

我想知道如何在UIView中删除路径。

以下是我的问题的简要说明 -

我有两个UIView,一个是红色,另一个是绿色背景。 绿色的是红色的。 因此,如果用户触摸并移动绿色UIView中的手指,则应该从UIView切出触摸的位置,并且应该从该区域看到红色UIView。 / p>

这是一张可以轻松描述问题的图片 - Visual description

我知道如何获得用户的手指触摸位置,但不知道如何从UIView切出路径。

请帮忙。

1 个答案:

答案 0 :(得分:-1)

我只使用1个视图。你也可以这样做。

夫特:

import UIKit

class PanView: UIView {
  override init(frame: CGRect) {
    super.init(frame: frame)
    _init()
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    _init()
  }

  func _init() {
    layer.backgroundColor = UIColor.greenColor().CGColor

    let green = CALayer()
    green.frame = layer.bounds
    green.backgroundColor = UIColor.redColor().CGColor
    layer.addSublayer(green)

    let mask = CAShapeLayer()
    mask.frame = green.bounds
    mask.strokeColor = UIColor.whiteColor().CGColor
    mask.fillColor = UIColor.clearColor().CGColor
    mask.lineWidth = 10
    mask.lineJoin = kCALineJoinRound
    let path = UIBezierPath()
    mask.path = path.CGPath
    green.mask = mask

    let pan = UIPanGestureRecognizer(target: self, action: "pan:")
    addGestureRecognizer(pan)
  }

  func pan(pan: UIPanGestureRecognizer) {
    let p = pan.locationInView(self)
    if let mask = layer.sublayers?.first?.mask as? CAShapeLayer, path = mask.path {
      let path = UIBezierPath(CGPath: path)
      if pan.state == UIGestureRecognizerState.Began {
        path.moveToPoint(p)
      } else {
        path.addLineToPoint(p)
      }
      mask.path = path.CGPath
    }
  }
}

目标-C:

#import "PanView.h"

@implementation PanView

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [self _init];
  }
  return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
  self = [super initWithCoder:coder];
  if (self) {
    [self _init];
  }
  return self;
}

- (void)_init {
  self.layer.backgroundColor = [UIColor greenColor].CGColor;

  CALayer *green = [CALayer layer];
  green.frame = self.layer.bounds;
  green.backgroundColor = [UIColor redColor].CGColor;
  [self.layer addSublayer:green];

  CAShapeLayer *mask = [CAShapeLayer layer];
  mask.frame = green.bounds;
  mask.strokeColor = [UIColor whiteColor].CGColor;
  mask.fillColor = [UIColor clearColor].CGColor;
  mask.lineWidth = 10;
  mask.lineJoin = kCALineJoinRound;
  UIBezierPath *path = [UIBezierPath bezierPath];
  mask.path = path.CGPath;
  green.mask = mask;

  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  [self addGestureRecognizer:pan];
}

- (void)pan:(UIPanGestureRecognizer *)pan {
  CGPoint p = [pan locationInView:self];
  CALayer *green = (id)self.layer.sublayers.firstObject;
  CAShapeLayer *mask = (id)green.mask;
  UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:mask.path];
  if (pan.state == UIGestureRecognizerStateBegan) {
    [path moveToPoint:p];
  } else {
    [path addLineToPoint:p];
  }
  mask.path = path.CGPath;
}

@end