2010-07-17 29 views
0

嗨,我想要一點東西的幫助。在我的應用程序中,我有一個用代表圖像的自定義單元格填充的UITableView。 (即,選擇一行顯示圖像視圖中的圖片)。類似iPhone上的Interface Builder連接線的效果

我想要做的是在自定義單元中有一個圖標,我可以拖動到一系列圖像視圖中的一個。類似於您可以在IB中拖動一條線來設置連接的方式。一旦用戶釋放他們的手指,我將檢查他們釋放它的屏幕的哪一部分,並且如果它是表示這些相框的這些矩形中的一個,它將用圖像填充相框並且該線將消失。

我從來沒有在我的應用程序之前畫過線,所以那不是我知道該怎麼做(所以即時通訊只是尋找一個教程或類定義的鏈接),第二,自從開始的時候我會有什麼問題該行在UITableViewCell中,終點位於主視圖中?

回答

0

其實我已經做過,所以我可以給你確切的代碼:d

這僅僅是繪圖部分,您實現了觸摸事件(在一個單獨的類,否則刪除self.userInteractionEnabled = NO;在.m文件

的BOOL拖告知用戶,如果線應繪製的LineView

LineView.h

#import <UIKit/UIKit.h> 

@interface LineView : UIView 
{ 
    CGPoint start; 
    CGPoint end; 

    BOOL dragged; 
} 

@property CGPoint start, end; 
@property BOOL dragged; 

@end 

LineView.m

#import "LineView.h" 

@implementation LineView 

@synthesize start, end, dragged; 

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame: frame]) 
    { 
     self.userInteractionEnabled = NO; 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

#pragma mark Setters 
-(void) setStart: (CGPoint) s 
{ 
    start = s; 
    [self setNeedsDisplay]; 
} 

-(void) setEnd: (CGPoint) e 
{ 
    end = e; 
    [self setNeedsDisplay]; 
} 

#define LINE_COLOR [UIColor redColor] 
#define CIRCLE_COLOR [UIColor redColor] 

#define LINE_WIDTH 5 
#define CIRCLE_RADIUS 10 

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef c = UIGraphicsGetCurrentContext(); 

    if(dragged) { 
     [LINE_COLOR setStroke]; 
     CGContextMoveToPoint(c, start.x, start.y); 
     CGContextAddLineToPoint(c, end.x, end.y);   
     CGContextSetLineWidth(c, LINE_WIDTH); 
     CGContextClosePath(c); 
     CGContextStrokePath(c); 

     [CIRCLE_COLOR setFill]; 
     CGContextAddArc(c, start.x, start.y, CIRCLE_RADIUS, 0, M_PI*2, YES); 
     CGContextClosePath(c); 
     CGContextFillPath(c); 

     CGContextAddArc(c, end.x, end.y, CIRCLE_RADIUS, 0, M_PI*2, YES); 
     CGContextClosePath(c); 
     CGContextFillPath(c); 
    } 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 
+0

我希望你知道,這種觀點必須高於所有其他意見。 要檢測線被拖動到哪裏,請使用touchesEnded:在處理觸摸事件的類中 – tadejsv 2010-07-17 20:03:28

+0

哇,這太好了,我會試一試,讓你知道 – Brodie 2010-07-17 20:07:06

+0

好吧,我已經開始工作,並意識到我知道比我需要的少得多。當用戶將他們的手指拖出tableViewCell中的按鈕時,我是否實現了這一點? – Brodie 2010-07-17 21:43:36

相關問題