2011-02-02 68 views
0

有人能指教一下方法用於以下情形:繪製動畫的行刪除自身

用戶觸摸屏幕,然後從這個點拖動他們的手指離開,並繪製一條線,但是,當他們把他們的上下手指沿着他們的手指直線。

示例就像時鐘的中心,分針固定在中心,用戶在另一端拖動時鐘。分針作爲永久性直線滑過,不會擦除其下面的細節...。

希望我解釋說得對嗎?

感謝一些指針

+0

您必須瞭解有關繪製直線的Quartz。 只需使用touchesBegan在變量CGPoint中設置起點。 在touchesMoved中,在另一個CGPoint變量中設置終點,然後使用setNeedsDisplay重繪該行。 您必須瞭解如何使用drawRect和setNeedsDisplay。 – Fattie 2016-04-23 11:45:16

回答

0

只是保存在x /是拖動開始的點的y座標,並且其中所述手指是後幾毫秒的點,然後計算方向向量

http://en.wikipedia.org/wiki/Direction_vector

與計算出的公式中,總是隻取手指x或y位置,並且用它來計算所述對方

+0

@joe,是的,我看到我誤解了這個問題。沒有讀取擦除,並重新繪製到新的手指位置部分 – cppanda 2011-02-02 07:51:52

2

使用以下代碼:

- (void)drawRect:(CGRect)rect { 

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); 
    CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinMiter); 

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0); 
    CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x,endPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    self.image = UIGraphicsGetImageFromCurrentImageContext(); 

    [self setNeedsDisplay]; 

}