2012-08-07 28 views
2

我想做一個類似於Scalar應用程序的類似的東西,他們在這裏做了一個從點拖動到記事本上的功能,將數字粘貼到他們拖動的位置點也是。什麼我真正感興趣的是,保持連接點和你的手指如下所示的行:在點和你的手指之間建立一條不變的線

enter image description here

我的問題是,我真的不知道這是什麼所謂的,所以我無法搜索我將如何做到這一點。有人知道這會被稱爲什麼,有任何教程他們遇到這個問題?甚至更好,如果你有一些代碼讓我看看會很棒。

謝謝。

+3

你或許可以用完成繪製CGContext上的http://developer.apple.com/library/ios/#DOCUMENTATION/GraphicsImaging/Reference/CGContext/Reference/reference.html同樣的事情。我不認爲有關這個特定主題的任何教程;這是你必須自己編碼的東西。根據你的經驗,這可能很困難。 – Dustin 2012-08-07 12:17:42

回答

1

UIView的這個例子,在手指被拖拽時畫線並檢測到要觸摸的第一個視圖應該可以幫助你開始。

//this goes in the header file called "UILineView.h" 

#import <UIKit/UIKit.h> 

@interface UILineView : UIView 

@end 

//this in the implementation file called "UILineView.m" 

#import "UILineView.h" 

@implementation UILineView 

{ 
    CGPoint _originOfTouchPoint; // your fist touch detected in touchesBegan: method 
    CGPoint _currentFingerPositionPoint; // the position you have dragged your finger to 
    CGFloat _strokeWidth; // the width of the line you wish to draw 
    id _touchStartedObject; // the object(UIView) that the first touch was detected on 
} 

// If you use Interface Builder to design your interface, Objects in a nib file are reconstituted and then initialized using 
// their initWithCoder: method 
- (id)initWithCoder:(NSCoder *)decoder 
{ 
    self = [super initWithCoder:decoder]; 
    if (self) { 
     // Initialization code 
     _originOfTouchPoint = CGPointMake(0.0, 0.0); 
     _currentFingerPositionPoint = CGPointMake(100.0, 100.0); 
     _strokeWidth = 2.0; 
    } 
    return self; 
} 
/* 
// Use initWithFrame if you are not loding the UIView from a nib file 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     _originOfTouchPoint = CGPointMake(0.0, 0.0); 
     _currentFingerPositionPoint = CGPointMake(100.0, 100.0); 
     _strokeWidth = 2.0; 
    } 
    return self; 
} 
*/ 

// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGContextSetLineWidth(context, _strokeWidth); 
    // fisrt point of line 
    CGContextMoveToPoint(context, _originOfTouchPoint.x, _originOfTouchPoint.y); 
    // last point of line 
    CGContextAddLineToPoint(context, _currentFingerPositionPoint.x, _currentFingerPositionPoint.y); 
    // draw the line 
    CGContextStrokePath(context); 
} 

#pragma mark touches 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // get starting point and first view touched (if you need to send that view messages) 
    _originOfTouchPoint = [[touches anyObject] locationInView:self]; 
    _touchStartedObject = [[touches anyObject] view]; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint movedToPoint = [[touches anyObject] locationInView:self]; 
    // if moved to a new point redraw the line 
    if (CGPointEqualToPoint(movedToPoint, _currentFingerPositionPoint) == NO) 
    { 
     _currentFingerPositionPoint = movedToPoint; 
     // calls drawRect: method to show updated line 
     [self setNeedsDisplay]; 
    } 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // reset values 
    _originOfTouchPoint = CGPointZero; 
    _currentFingerPositionPoint = CGPointZero; 
    _touchStartedObject = nil; 
} 

@end 
+0

你的男人!謝謝 – Craig 2012-08-07 17:57:00

+0

問題:如果我想限制它只在單擊某個位置或單擊某個按鈕時才能工作,我該怎麼辦? – Craig 2012-08-07 18:10:08

+0

您可以在touchesBegun中讀取_touchStartedObject:如果它是您想要的對象(UIButton或UIView),則可以將布爾變量設置爲YES。檢查這個變量在drawRect中設置爲YES: – 2012-08-07 18:18:18

1

它將涉及子類UIView和implemeting的touchesBegantouchesMoved方法和從初始觸摸到您的當前觸摸畫線在視圖中的子類drawRect方法。

繼承人先前的問題,這將有助於How do I draw a line on the iPhone?

你只需要改變以下,這樣的座標是那些您最初的接觸和當前觸摸的,你從接觸方法獲得

CGContextMoveToPoint(c, 5.0f, 5.0f); 
CGContextAddLineToPoint(c, 50.0f, 50.0f); 

然後在touchesMoved方法中調用[self setNeedsDisplay];將會重繪線條,以便在移動時跟隨手指。

然後執行touchesEnded獲取手指擡起時的代碼。

Hoope it help!

0

A UIGestureRecognizer可以做到這一點,而不會混淆觸摸和拖動邏輯與視圖。我用它們來編輯地圖上的形狀,長按拖拽等。請參閱this tutorial