0

我需要通过在 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];
4

2 回答 2

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
于 2013-02-18T09:39:11.430 回答
0

从代码中看不太清楚,但根据视频和预感,我猜用手指的单次滑动最终会变成几条线段。这意味着冲刺从头开始多次,在视频中创建效果。您可能需要一个 NSMutableArray 将所有这些 currentPoint 放入其中,然后每次重绘时将它们全部绘制为一条线。

这个问题有更多关于如何以这种方式画线的信息:iPhone Core Graphics粗虚线子视图

于 2013-02-15T10:55:23.203 回答