AJP*_*tel 2 iphone objective-c ios4
嘿我是目标C的初学者请帮助我
我制作以下代码但不工作.....
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self.view) {
CGPoint location = [touch locationInView:self.view];
loc1 = location;
CGContextMoveToPoint(context, location.x, location.y);
NSLog(@"x:%d y:%d At Touch Begain", loc1.x, loc1.y);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self.view)
{
CGPoint location = [touch locationInView:self.view];
CGContextMoveToPoint(context, loc1.x, loc1.y);
NSLog(@"x:%d y:%d At Touch Move", loc1.x, loc1.y);
CGContextAddLineToPoint(context, location.x, location.y);
NSLog(@"x:%d y:%d", location.x, location.y);
}
}
Run Code Online (Sandbox Code Playgroud)
我在viewdidload方法中声明了contex,并尝试声明触摸事件但不工作...
我的应用日志文件看起来像......
x:0 y:1079934976在Touch Move Thu 1月13日11:20:05 .local DragDrop [536]:
CGContextAddLineToPoint:无效的上下文0x0 2011-01-13 11:20:05.149 DragDrop [536:207] x:0 y:1079902208 Thu Jan 13 11:20:05 .local DragDrop [536]:
CGContextSetRGBStrokeColor:无效的上下文0x0 Thu Jan 13 11:20:05 .local DragDrop [536]:
CGContextDrawPath:无效的上下文0x0 Thu Jan 13 11:20:05 .local DragDrop [536]:
CGContextMoveToPoint:无效的上下文0x0 2011-01-13 11:20:05.199 DragDrop [536:207] x:0 y:1079934976在Touch Move Thu 1月13日11:20:05 .local DragDrop [536]:
CGContextAddLineToPoint:无效的上下文0x0 2011-01-13 11:20:05.200 DragDrop [536:207] x:0 y:1079885824
omz*_*omz 14
您通常不会直接在触摸检测方法中绘制.通常,您只需在那里存储新的点/线并将其全部绘制出来drawRect:.让我们假设你有一个自定义的子类,UIView它有一个实例变量paths类型NSMutableArray和属性currentPath类型UIBezierPath.然后,您可以大致像这样实现触摸检测和drawRect方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.currentPath = [UIBezierPath bezierPath];
currentPath.lineWidth = 3.0;
[currentPath moveToPoint:[touch locationInView:self]];
[paths addObject:self.currentPath];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self.currentPath addLineToPoint:[touch locationInView:self]];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] set];
for (UIBezierPath *path in paths) {
[path stroke];
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这简化了很多.如果你绘制很多行,性能将受到影响,最终,你需要将绘图缓存在位图图像中,但这应该让你开始.
| 归档时间: |
|
| 查看次数: |
8714 次 |
| 最近记录: |