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

コードからはかなり不明確ですが、ビデオと予感に基づいて、指で1回スワイプすると複数の線分になると思います。これは、ダッシュが最初から数回開始され、ビデオに効果が生じることを意味します。おそらく、これらすべてのcurrentPointを配置するNSMutableArrayが必要であり、再描画するたびにそれらすべてを1本の線として描画します。

この質問には、この方法で線を描画する方法に関する詳細情報があります。iPhoneCoreGraphicsのサブビュー用の太い破線

于 2013-02-15T10:55:23.203 に答える
0

ここにあなたがしようとしていることをするためのコードがあります.CoreGraphicsの代わりにQuartzを使用しています. 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 に答える