如何在iPhone应用程序中用手指绘制虚线?

时间:2013-02-06 12:40:18

标签: ios drawing core-graphics

我需要在iPhone屏幕上滑动手指,在常规间隙画一条破折线。我已经为此编写了以下代码,它正在绘制虚线,但是间隙不规则,因此看起来不太好。我还提供了显示问题的视频链接请帮忙!

视频链接: https://dl.dropbox.com/u/56721867/Screen%20Recording%207.mov

   CGPoint mid1 = midPoint__5(previousPoint1, previousPoint2);
   CGPoint mid2 = midPoint__5(currentPoint, previousPoint1);
   CGContextRef context = UIGraphicsGetCurrentContext();
   [self.layer renderInContext:context];
   self.lineColor=[arrObj objectAtIndex:colorTag];
   CGContextMoveToPoint(context, mid1.x, mid1.y);
   CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
   CGContextSetLineCap(context, kCGLineCapRound);//use for making circle ,rect or line effect===============
   CGFloat dashArray[] = {2,8,2,8};
   CGContextSetLineDash(context,2,dashArray, 4);
   CGContextSetLineWidth(context, self.lineWidth);
   self.lineColor=[arrObj objectAtIndex:[AppHelper userDefaultsForKey:@"ColorTag"].intValue];//color tag on button click
   CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
   CGContextSetAlpha(context, 1.0);
   CGBlendMode blendStyle=isErase?kCGBlendModeClear:kCGBlendModeNormal; 
   CGContextSetBlendMode(context,blendStyle);
   CGContextStrokePath(context);
   [super drawRect:rect];

2 个答案:

答案 0 :(得分:0)

从代码中相当不清楚,但基于视频和预感,我猜测用手指轻轻滑动最终会成为几个线段。这意味着破折号从头开始几次,在视频中创建效果。您可能需要一个NSMutableArray,将所有currentPoint放入其中,然后在每次重绘时将它们全部绘制为一行。

此问题提供了有关如何以这种方式绘制线条的更多信息:iPhone Core Graphics thicker dashed line for subview

答案 1 :(得分:0)

这是执行您正在尝试的代码,它使用Quartz而不是CoreGraphics。您可以使用Quartz或CoreGraphics。

//
//  TestView.h
// 
//
//  Created by Syed Arsalan Pervez on 2/18/13.
//  Copyright (c) 2013 SAPLogix. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TestView : UIView
{
    NSMutableArray *_points;
}

@end


//
//  TestView.m
// 
//
//  Created by Syed Arsalan Pervez on 2/18/13.
//  Copyright (c) 2013 SAPLogix. All rights reserved.
//

#import "TestView.h"

@interface UserPoint : NSObject
{
    BOOL _start;
    BOOL _end;
    CGPoint _point;
}

@property (assign, nonatomic) BOOL start;
@property (assign, nonatomic) BOOL end;
@property (assign, nonatomic) CGPoint point;

@end

@implementation UserPoint

@end

@implementation TestView

- (id)init
{
    self = [super init];
    if (self)
    {
        _points = [NSMutableArray new];
    }
    return self;
}

- (UserPoint *)addPoint:(CGPoint)point
{
    UserPoint *_userPoint = [UserPoint new];
    _userPoint.point = point;
    [_points addObject:_userPoint];
    [_userPoint release];

    return [_points lastObject];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UserPoint *_userPoint = [self addPoint:[touch locationInView:self]];
    _userPoint.start = YES;
    _userPoint = nil;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UserPoint *_userPoint = [self addPoint:[touch locationInView:self]];
    _userPoint = nil;

    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UserPoint *_userPoint = [self addPoint:[touch locationInView:self]];
    _userPoint.end = YES;
    _userPoint = nil;

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [[UIColor blackColor] setStroke];
    [[UIColor blackColor] setFill];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path setLineWidth:15];
    CGFloat dash[] = {15,20};
    [path setLineDash:dash count:2 phase:0];
    [path setLineCapStyle:kCGLineCapRound];
    CGPoint lastPoint;
    for (UserPoint *_point in _points)
    {
        if (_point.start || _point.end)
            [path moveToPoint:_point.point];
        else
        {
            [path addQuadCurveToPoint:_point.point controlPoint:lastPoint];
        }
        lastPoint = _point.point;
    }
    [path stroke];
}

- (void)dealloc
{
    [_points release];

    [super dealloc];
}

@end