重新定位/调整UIBezierPath的大小

时间:2013-12-05 12:44:25

标签: ios iphone ios7 mask uibezierpath

我有一个可拖动的视图,上面有一个遮罩层。掩码层上有一个UIBezierPath,它使视图上的区域看透/透明(我想要的效果)。我的最终目标是通过传递一个基于我的视图和另一个矩形的交集计算的CGRect来改变路径的位置和大小(不是遮罩层!)。基本上我想隐藏相交的区域。 / p>

1)我如何创建蒙版和路径(在我的视图上创建一个透明矩形):

self.maskLayer = [CAShapeLayer layer];
self.maskLayer.frame = self.contentView.bounds;

//default path rect
CGRect const rect = CGRectMake(CGRectGetMidX(self.contentView.bounds) ,
                                     CGRectGetMidY(self.contentView.bounds),
                                     50,50);


path = [UIBezierPath bezierPathWithRect:rect];

[path appendPath:[UIBezierPath bezierPathWithRect:self.contentView.frame]];

self.maskLayer.path = path.CGPath;

self.maskLayer.fillRule = kCAFillRuleEvenOdd;

self.contentView.layer.mask = self.maskLayer;

2)我的问题是我找不到一种方法来调整/重新定位路径。我在网上搜索但找不到任何解决方案。

这段代码不起作用,但我能想到的是:

//runs while the view is being dragged(using UIPanGestureRecognizer)
-(void)move{
 CGRect intersectedRect = CGRectIntersection(self.frame, self.productView.frame);
 path = [UIBezierPath bezierPathWithRect:intersectedRect];
[path appendPath:[UIBezierPath bezierPathWithRect:intersectedRect]];
[self.maskLayer setNeedsDisplay];
 }

如何在移动功能中仅操纵遮罩层的路径? 谢谢!

6 个答案:

答案 0 :(得分:17)

// example path
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) cornerRadius:4];

// scale it
CGFloat scale = 0.9;
[path applyTransform:CGAffineTransformMakeScale(scale, scale)];

// move it
CGSize translation = CGSizeMake(10, 5);
[path applyTransform:CGAffineTransformMakeTranslation(translation.width,
                                                      translation.height)];

// apply it
self.myLayer.path = path;

答案 1 :(得分:10)

Swift版本

这是@ hfossli的Swift代码:

// example path
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 100), cornerRadius: 4)

// scale it
let scale = CGFloat(0.9)
path.applyTransform(CGAffineTransformMakeScale(scale, scale))

// move it
let translation = CGSize(width: 10, height: 5)
path.applyTransform(CGAffineTransformMakeTranslation(translation.width,
    translation.height))

// apply it
self.myLayer.path = path

<强>更新

我在实际练习中发现,在创建UIBezierPath而不是稍后对其进行转换时,我只是提供了不同的初始值。我想这取决于目的。

答案 2 :(得分:2)

快速进入3

func move(path: UIBezierPath, fromPoint: CGPoint, toPoint: CGPoint) {
    let moveX = toPoint.x - fromPoint.x
    let moveY = toPoint.y - fromPoint.y
    path.apply(CGAffineTransform(translationX: moveX, y: moveY))
}

func scale(path: UIBezierPath, fromSize: CGSize, toSize: CGSize) {
    if fromSize.width == 0 || fromSize.height == 0 {
        return
    }
    let scaleWidth = toSize.width / fromSize.width
    let scaleHeight = toSize.height / fromSize.height
    path.apply(CGAffineTransform(scaleX: scaleWidth, y: scaleHeight))
}

答案 3 :(得分:2)

您需要在移动方法中将更新后的路径分配给self.maskLayer.path

答案 4 :(得分:1)

Swift 3扩展(如果需要,使用factorX,factorY修改它):

$ httpd -t -D DUMP_MODULES | grep header
Syntax OK
headers_module (shared)

答案 5 :(得分:0)

快捷键4

if (page && page == 'ownerDashboard')