iphone - 如何使用 UIBezierPath 绘制一条跟随触摸事件的线?

标签 iphone objective-c ios cocoa-touch

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

- (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 实现此线条绘制?

最佳答案

#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

关于iphone - 如何使用 UIBezierPath 绘制一条跟随触摸事件的线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7393058/

相关文章:

iphone - AFNetworking 自动将请求包装在 JSON 中?

ios - Core Data 是否保留 NSMutableArray 中对象的顺序?

ios - 用于填充 UITableView 的 NSMutableArray 属性

ios - UILabel 未在详细 View 中显示

ios - 如何检查 CLLocationCoordinate2D 是否在四个 CLLocationCoordinate2D 正方形内?在带有谷歌地图的 Objective C 中

ios - 如何使用 google-plus-ios-sdk-1.7.1 sdk 登录 google-plus?

iphone - 链接具有可达性的 UITableView 单元格

ios - 如何调整图像大小以适应 UITableView 单元格?

javascript - Swift stringByEvaluatingJavaScriptFromString

iphone - 如何在更改方向时调整 View 组件的大小