【问题标题】:How do I use UIBezierPath to draw a line that follows a touch event?如何使用 UIBezierPath 绘制跟随触摸事件的线?
【发布时间】:2011-09-12 19:41:29
【问题描述】:

我从一个类似的问题中选择了以下代码,但我没有任何运气让它工作。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"Check #1");

    UITouch* touch = [touches anyObject];
    self.currentPath = [UIBezierPath bezierPath];
    currentPath.lineWidth = 3.0;
    [currentPath moveToPoint:[touch locationInView:self.view]];
    [paths addObject:self.currentPath];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self.currentPath addLineToPoint:[touch locationInView:self.view]];
    [self.view setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [[UIColor redColor] set];
    for (UIBezierPath *path in paths) {
        NSLog(@"Check #2?");
        [path stroke];
    }
}

我从中得到的是我需要使用UIBezierPath,但我不确定如何让它跟随我的手指。我只想画一条线,在用户触摸时开始,在他们拿起手指的地方结束。

如何使用 UIBezierPath 实现这种线条绘制?

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch


    【解决方案1】:
    #import <UIKit/UIKit.h>
    
    @interface Canvas : UIImageView
    
    @property (nonatomic, assign) CGPoint location;
    
    @end
    
    
    
    
    #import "Canvas.h"
    
    @implementation Canvas
    
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        self.location = [touch locationInView:self];
    }
    
    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint currentLocation = [touch locationInView:self];
    
        UIGraphicsBeginImageContext(self.frame.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(ctx, kCGLineCapRound);
        CGContextSetLineWidth(ctx, 5.0);
        CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, self.location.x, self.location.y);
        CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
        CGContextStrokePath(ctx);
        self.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        self.location = currentLocation;
    }
    
    - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint currentLocation = [touch locationInView:self];
    
        UIGraphicsBeginImageContext(self.frame.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(ctx, kCGLineCapRound);
        CGContextSetLineWidth(ctx, 5.0);
        CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, self.location.x, self.location.y);
        CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
        CGContextStrokePath(ctx);
        self.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        self.location = currentLocation;
    }
    
    @end
    

    【讨论】:

    • 附带说明,多次使用 UIGraphicsGetCurrentContext() 可能是多余的。声明一个 CGContextRef 变量并引用它。
    • 可能是的。这只是我在目录中找到的一个旧示例。在弄清楚 Core Graphics 的早期,我从未将它用于其他任何事情。 (我已经更新了示例)
    • 我猜UIGraphicsGetCurrentContext()应该介于UIGraphicsBeginImageContext()和UIGraphicsEndImageContext()之间,否则为NULL。
    • 是的,可能应该。我移动了它。
    • 不要忘记添加 imageView.userInteractionEnabled = YES。默认值为 NO,这意味着视图不响应触摸。
    猜你喜欢
    • 2012-04-03
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多