其實我已經做過,所以我可以給你確切的代碼: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
我希望你知道,這種觀點必須高於所有其他意見。 要檢測線被拖動到哪裏,請使用touchesEnded:在處理觸摸事件的類中 – tadejsv 2010-07-17 20:03:28
哇,這太好了,我會試一試,讓你知道 – Brodie 2010-07-17 20:07:06
好吧,我已經開始工作,並意識到我知道比我需要的少得多。當用戶將他們的手指拖出tableViewCell中的按鈕時,我是否實現了這一點? – Brodie 2010-07-17 21:43:36