2013-04-24 53 views
1

我有一個或多或少具有弧形的貝塞爾路徑,事情是我希望用戶能夠在弧內點擊(其中它可能是一個更清晰的門),但不在弧上。但我不能這樣做,就像弧線內的空間也是貝塞爾路徑的一部分,但唯一的一點是它是透明的。貝塞爾路徑的透明區域不允許用戶交互

它創建BezierPath的代碼如下:

- (UIImageView*)drawBezierPath { 
    //// Bezier Drawing 
    UIBezierPath* bezierPath = [UIBezierPath bezierPath]; 
    [bezierPath moveToPoint: CGPointMake(55.5, 643.5)]; 
    [bezierPath addLineToPoint: CGPointMake(55.5, 417)]; 
    [bezierPath addLineToPoint: CGPointMake(63, 399)]; 
    [bezierPath addLineToPoint: CGPointMake(69.5, 381.5)]; 
    [bezierPath addLineToPoint: CGPointMake(79, 363.5)]; 
    [bezierPath addLineToPoint: CGPointMake(91, 346)]; 
    [bezierPath addLineToPoint: CGPointMake(102, 331)]; 
    [bezierPath addLineToPoint: CGPointMake(114.5, 314.5)]; 
    [bezierPath addLineToPoint: CGPointMake(127.5, 301.5)]; 
    [bezierPath addLineToPoint: CGPointMake(142.5, 286)]; 
    [bezierPath addLineToPoint: CGPointMake(156.5, 273.5)]; 
    [bezierPath addLineToPoint: CGPointMake(171, 260)]; 
    [bezierPath addLineToPoint: CGPointMake(189.5, 247.5)]; 
    [bezierPath addLineToPoint: CGPointMake(207.5, 234.5)]; 
    [bezierPath addLineToPoint: CGPointMake(229.5, 222.5)]; 
    [bezierPath addLineToPoint: CGPointMake(250, 211)]; 
    [bezierPath addLineToPoint: CGPointMake(272.5, 202)]; 
    [bezierPath addLineToPoint: CGPointMake(295.5, 194)]; 
    [bezierPath addLineToPoint: CGPointMake(321, 186.5)]; 
    [bezierPath addLineToPoint: CGPointMake(349, 180.5)]; 
    [bezierPath addLineToPoint: CGPointMake(375, 179)]; 
    [bezierPath addLineToPoint: CGPointMake(398, 177.5)]; 
    [bezierPath addLineToPoint: CGPointMake(424.5, 177.5)]; 
    [bezierPath addLineToPoint: CGPointMake(448.5, 180.5)]; 
    [bezierPath addLineToPoint: CGPointMake(473, 184.5)]; 
    [bezierPath addLineToPoint: CGPointMake(498.5, 192)]; 
    [bezierPath addLineToPoint: CGPointMake(521.5, 200)]; 
    [bezierPath addLineToPoint: CGPointMake(544.5, 209.5)]; 
    [bezierPath addLineToPoint: CGPointMake(565.5, 220)]; 
    [bezierPath addLineToPoint: CGPointMake(584, 231)]; 
    [bezierPath addLineToPoint: CGPointMake(603, 244)]; 
    [bezierPath addLineToPoint: CGPointMake(623.5, 259)]; 
    [bezierPath addLineToPoint: CGPointMake(640.5, 274)]; 
    [bezierPath addLineToPoint: CGPointMake(657.5, 290.5)]; 
    [bezierPath addLineToPoint: CGPointMake(673, 308)]; 
    [bezierPath addLineToPoint: CGPointMake(688.5, 327)]; 
    [bezierPath addLineToPoint: CGPointMake(702.5, 346.5)]; 
    [bezierPath addLineToPoint: CGPointMake(715, 368)]; 
    [bezierPath addLineToPoint: CGPointMake(727, 392.5)]; 
    [bezierPath addLineToPoint: CGPointMake(736.5, 414.5)]; 
    [bezierPath addLineToPoint: CGPointMake(736.5, 644)]; 
    [bezierPath addLineToPoint: CGPointMake(789.5, 644.5)]; 
    [bezierPath addLineToPoint: CGPointMake(789.5, 1)]; 
    [bezierPath addLineToPoint: CGPointMake(2.5, 1)]; 
    [bezierPath addLineToPoint: CGPointMake(2, 645)]; 
    [bezierPath addLineToPoint: CGPointMake(55.5, 643.5)]; 
    [bezierPath closePath]; 
    [[UIColor blackColor] setFill]; 
    [bezierPath fill]; 
    [[UIColor blackColor] setStroke]; 
    bezierPath.lineWidth = 1; 
    [bezierPath stroke]; 

    UIGraphicsBeginImageContext(CGSizeMake(800, 800)); 

    //this gets the graphic context 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    //you can stroke and/or fill 
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
    [bezierPath fill]; 
    [bezierPath stroke]; 

    //now get the image from the context 
    UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage]; 
    bezierImageView.alpha = 0; 
    bezierImageView.frame = CGRectMake(115, 92, bezierImageView.frame.size.width, bezierImageView.frame.size.height); 
    return bezierImageView; 
} 

在該方法我創建bezierPath和它在一個UIImageView保存它添加到另一個視圖。

我以爲bezierPath用於繪製奇怪的形式,只佔用那種形式的空間,但現在我不知道該怎麼想!

有關於此的任何想法?謝謝!

+0

你想做什麼?你想找BezierPath嗎? – 2013-04-24 19:17:47

+0

我希望bezierPath不允許觸及其區域,但是在其餘空間中是的。 – diegomen 2013-04-25 13:31:22

回答

3

第1步:在項目中添加QuartzCore.Framwork

第2步:#import <QuartzCore/QuartzCore.h>在.h文件中

第3步:在您的.h文件中定義一個變量UIBezierPath *path;

第4步:在您的viewDidLoad方法中繪製一個Trianble形狀

path = [UIBezierPath bezierPath]; 
[path moveToPoint:CGPointMake(100, 100)]; 
[path addLineToPoint:CGPointMake(50, 200)]; 
[path addLineToPoint:CGPointMake(150, 200)]; 
[path closePath]; 
[path setLineWidth:2.0f]; 

// Draw a shape Layer with Triangle Path 
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.fillColor = [UIColor redColor].CGColor; 
shapeLayer.strokeColor = [UIColor yellowColor].CGColor; 
shapeLayer.lineWidth = 2; 
shapeLayer.path = path.CGPath; 

// Add it to your view 
[self.view.layer addSublayer:shapeLayer]; 

第5步:實現以下觸摸方法來檢測觸摸上查看&形狀

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    CGPoint currentPoint = [touch locationInView:self.view]; 
    if([path containsPoint:currentPoint]) 
    { 
     NSLog(@"its Inside Triangle Path"); 
    } 
    else 
    { 
     NSLog(@"OutSide Shape"); 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    if([path containsPoint:currentPoint]) 
    { 
     NSLog(@"its Inside Triangle Path"); 
    } 
    else 
    { 
     NSLog(@"OutSide Shape"); 
    } 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    if([path containsPoint:currentPoint]) 
    { 
     NSLog(@"its Inside Triangle Path"); 
    } 
    else 
    { 
     NSLog(@"OutSide Shape"); 
    } 
} 

您可以創建這樣的任何形狀和進行比較。

+0

最後我用了touchesBegan方法,但我不需要三角形的形狀。非常感謝您的回答! – diegomen 2013-04-27 14:54:55

+0

它也適用於我。非常感謝.. :) – Swati 2013-05-08 08:23:33

+0

歡迎:) – 2013-05-08 10:15:09

0

您需要在視圖中查找觸摸事件(例如TouchesBegan,TouchesMoved,TouchesEnded,TouchesCancelled)。觸摸發生時,您可以在視圖中獲取觸摸位置。您可以使用此位置並檢查該位置是否在您的路徑內。

看到這個職位:Path Interaction

0

如何繪製視圖(您的貝塞爾路徑)與它們如何與用戶交互(觸摸)沒有多大關係。

我認爲你應該在你的UIView子類中實現-pointInside:withEvent:,這個方法確定觸摸事件是否在視圖內。當點不應觸及時,您應該返回NO

-touchesBegan:這樣的方法也有效,但有點複雜。