<bdo id='X8nB5'></bdo><ul id='X8nB5'></ul>

<small id='X8nB5'></small><noframes id='X8nB5'>

<tfoot id='X8nB5'></tfoot>

      1. <legend id='X8nB5'><style id='X8nB5'><dir id='X8nB5'><q id='X8nB5'></q></dir></style></legend>
      2. <i id='X8nB5'><tr id='X8nB5'><dt id='X8nB5'><q id='X8nB5'><span id='X8nB5'><b id='X8nB5'><form id='X8nB5'><ins id='X8nB5'></ins><ul id='X8nB5'></ul><sub id='X8nB5'></sub></form><legend id='X8nB5'></legend><bdo id='X8nB5'><pre id='X8nB5'><center id='X8nB5'></center></pre></bdo></b><th id='X8nB5'></th></span></q></dt></tr></i><div id='X8nB5'><tfoot id='X8nB5'></tfoot><dl id='X8nB5'><fieldset id='X8nB5'></fieldset></dl></div>

        如何在触摸事件上画线?

        时间:2023-10-02
            <tfoot id='vAN3D'></tfoot>

              <bdo id='vAN3D'></bdo><ul id='vAN3D'></ul>

              <small id='vAN3D'></small><noframes id='vAN3D'>

                <i id='vAN3D'><tr id='vAN3D'><dt id='vAN3D'><q id='vAN3D'><span id='vAN3D'><b id='vAN3D'><form id='vAN3D'><ins id='vAN3D'></ins><ul id='vAN3D'></ul><sub id='vAN3D'></sub></form><legend id='vAN3D'></legend><bdo id='vAN3D'><pre id='vAN3D'><center id='vAN3D'></center></pre></bdo></b><th id='vAN3D'></th></span></q></dt></tr></i><div id='vAN3D'><tfoot id='vAN3D'></tfoot><dl id='vAN3D'><fieldset id='vAN3D'></fieldset></dl></div>
                  <legend id='vAN3D'><style id='vAN3D'><dir id='vAN3D'><q id='vAN3D'></q></dir></style></legend>
                    <tbody id='vAN3D'></tbody>
                  本文介绍了如何在触摸事件上画线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  嘿,我是 Objective C 的初学者,请帮帮我

                  Hey i m beginner of objective C Please Help me

                  我做了以下代码,但不起作用.....

                  i make following code but not work.....

                  -(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);
                  }
                  
                  }
                  

                  我在 viewdidload 方法中声明了 contex,并尝试在触摸事件中声明但不起作用...

                  i declare contex in viewdidload method and also try to declare in touch event but not work...

                  我的应用日志文件看起来像...

                  My app Log file look like...

                  x:0 y:1079934976 At Touch Move 周四 1 月 13 日 11:20:05 .local拖放[536]:

                  x:0 y:1079934976 At Touch Move Thu Jan 13 11:20:05 .local DragDrop[536] :

                  CGContextAddLineToPoint: 无效上下文 0x0 2011-01-13 11:20:05.149DragDrop[536:207] x:0 y:1079902208 Thu Jan 13 11:20:05 .local拖放[536]:

                  CGContextAddLineToPoint: invalid context 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 拖放[536]:

                  CGContextSetRGBStrokeColor: invalid context 0x0 Thu Jan 13 11:20:05 .local DragDrop[536] :

                  CGContextDrawPath: 无效上下文 0x0 Thu Jan 13 11:20:05 .local拖放[536]:

                  CGContextDrawPath: invalid context 0x0 Thu Jan 13 11:20:05 .local DragDrop[536] :

                  CGContextMoveToPoint: 无效上下文 0x0 2011-01-13 11:20:05.199DragDrop[536:207] x:0 y:1079934976 At Touch Move Jan 13 Thu 11:20:05.local 拖放[536]:

                  CGContextMoveToPoint: invalid context 0x0 2011-01-13 11:20:05.199 DragDrop[536:207] x:0 y:1079934976 At Touch Move Thu Jan 13 11:20:05 .local DragDrop[536] :

                  CGContextAddLineToPoint: 无效上下文 0x0 2011-01-13 11:20:05.200拖放[536:207] x:0 y:1079885824

                  CGContextAddLineToPoint: invalid context 0x0 2011-01-13 11:20:05.200 DragDrop[536:207] x:0 y:1079885824

                  推荐答案

                  您通常不会在触摸检测方法中直接绘图.通常,您只需在此处存储新的点/线,然后将它们全部绘制在 drawRect: 中.假设您有一个 UIView 的自定义子类,它有一个类型为 NSMutableArray 的实例变量 paths 和一个属性 currentPathUIBezierPath 类型.然后,您可以大致像这样实现触摸检测和 drawRect 方法:

                  You typically don't draw directly in the touch detection methods. Usually, you'd just store new points/lines there and draw all of them in drawRect:. Let's assume you have a custom subclass of UIView which has an instance variable paths of type NSMutableArray and a property currentPath of type UIBezierPath. You could then implement the touch detection and drawRect methods roughly like this:

                  - (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];
                    }
                  }
                  

                  请注意,这被简化了很多.如果你画了很多线,性能会受到影响,最终,你会希望将绘图缓存在位图图像中,但这应该可以帮助你开始.

                  Note that this is simplified a lot. If you draw many lines, performance will suffer and eventually, you'll want to cache the drawing in a bitmap image, but this should get you started.

                  这篇关于如何在触摸事件上画线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:iOS 下载并保存 HTML 文件 下一篇:如何在您的应用程序中接收远程控制事件的同时从 iPod 应用程序播放音乐?

                  相关文章

                  <legend id='Zt4bW'><style id='Zt4bW'><dir id='Zt4bW'><q id='Zt4bW'></q></dir></style></legend>
                    • <bdo id='Zt4bW'></bdo><ul id='Zt4bW'></ul>

                    <small id='Zt4bW'></small><noframes id='Zt4bW'>

                      <tfoot id='Zt4bW'></tfoot>

                    1. <i id='Zt4bW'><tr id='Zt4bW'><dt id='Zt4bW'><q id='Zt4bW'><span id='Zt4bW'><b id='Zt4bW'><form id='Zt4bW'><ins id='Zt4bW'></ins><ul id='Zt4bW'></ul><sub id='Zt4bW'></sub></form><legend id='Zt4bW'></legend><bdo id='Zt4bW'><pre id='Zt4bW'><center id='Zt4bW'></center></pre></bdo></b><th id='Zt4bW'></th></span></q></dt></tr></i><div id='Zt4bW'><tfoot id='Zt4bW'></tfoot><dl id='Zt4bW'><fieldset id='Zt4bW'></fieldset></dl></div>