touchesbegin, touchesmoved, touchesended 问题

Posted

技术标签:

【中文标题】touchesbegin, touchesmoved, touchesended 问题【英文标题】:touchesbegan, touchesmoved, touchesended issue 【发布时间】:2011-11-10 08:30:54 【问题描述】:

出于各种原因,我已将这些方法从 UIView 子类移至我的视图控制器。我终于让它工作了,除了一件事。我不仅可以拖动我以编程方式创建的 UIImageviews,而且实际的视图控制器视图也是可拖动的。造成这种不受欢迎的效果。我想这是它接触到任何物体的事实,而背景本身就是一个物体。我只是不确定如何排除背景。我认为它需要“启用用户交互”,但我猜不是?我只希望它使 UIImageViews 可拖动。请原谅我的菜鸟。我还在学习。

我在一个名为“letterDictionary”的 NSMutableDictionary 中拥有我想要“可触摸”的所有图像视图。是否可以仅将触摸应用于字典中的内容?

http://imgur.com/W08dI

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
    UITouch *touch = [touches anyObject];
    touchPoint = [touch locationInView:self.view];

    movingLetter = [touch view];

    CGPoint pointInside = [touch locationInView:[touch view]];
    if ([movingLetter pointInside:pointInside withEvent:event]) touchedInside = YES;




- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    if (touchedInside) 
        UITouch *touch = [touches anyObject];
        CGPoint newPoint = [touch locationInView:self.view];  // get the new touch location
        movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);         
        touchPoint = newPoint;      
    



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    if (touchedInside) 
        UITouch *touch = [touches anyObject];
        CGPoint newPoint = [touch locationInView:self.view];

        movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);

        if (CGRectIntersectsRect([movingLetter frame], [placeHolder frame]))
            
                movingLetter.center = placeHolder.center;
            

       
    touchedInside = NO;


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
    touchedInside = NO;

【问题讨论】:

【参考方案1】:

你有被触摸的视图,

    UITouch *touch = [touches anyObject];
    touchPoint = [touch locationInView:self.view];
    movingLetter = [touch view];

只需测试它是否是您要查找的类(例如UIImageView)然后返回

    UITouch *touch = [touches anyObject];
    if (![[touch view] isKindOfClass:[UIImageView class]])
    
       return;
    

【讨论】:

效果很好!我应该知道这样做!不过,出于好奇,您或其他人能否告诉我如何让它只为我的 NSMutableDictionary 中的对象(UIImageViews)添加触摸?【参考方案2】:

在这个 touchesBegen 方法中:

-(void)touchesBegan:(NSSet* )touches withEvent:(UIEvent *)event
 
     [self.view endEditing:YES];

     UITouch *touch = [touches anyObject];
    _previousPoint1 = [touch      previousLocationInView:self.main_uiview];
    _previousPoint2 = [touch previousLocationInView:self.main_uiview];
    _currentPoint = [touch locationInView:self.main_uiview];

    [self touchesMoved:touches withEvent:event];

    self.bezierPath = [UIBezierPath bezierPath];
    [self.bezierPath moveToPoint:_currentPoint];
     

TouchesMove 方法

  -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent* )event
   


    UITouch *touch = [touches anyObject];

    _previousPoint2 = _previousPoint1;
    _previousPoint1 = [touch previousLocationInView:self.main_uiview];
    _currentPoint = [touch locationInView:self.main_uiview];

    lastPoint = _currentPoint;

    [_bezierPath addLineToPoint:lastPoint];

    // calculate mid point
    CGPoint mid1 = midPoint4(_previousPoint1, _previousPoint2);
    CGPoint mid2 = midPoint4(_currentPoint, _previousPoint1);

    UIGraphicsBeginImageContextWithOptions(self.bg_imageview.frame.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context,brush);

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context,kCGLineJoinRound);

    [self.bg_imageview.image drawInRect:CGRectMake(0, 0, self.bg_imageview.frame.size.width, self.bg_imageview.frame.size.height)];

    CGContextMoveToPoint(context, mid1.x, mid1.y);

    // Use QuadCurve is the key
    CGContextAddQuadCurveToPoint(context, _previousPoint1.x, _previousPoint1.y, mid2.x, mid2.y);

    CGContextSetLineCap(context, kCGLineCapRound);

    CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);

    CGContextSetLineWidth(context, 3.0);


    CGContextStrokePath(context);

    self.bg_imageview.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

【讨论】:

以上是关于touchesbegin, touchesmoved, touchesended 问题的主要内容,如果未能解决你的问题,请参考以下文章

touchesBegin & touchesMove Xcode Obj C 问题

touchesBegin 在自定义 UITableViewCell,touchesEnded 在 UIView

Touchesbegin没有检测到被触摸的东西

TouchesMoved 与 Button?

Touchesmoved - 一次拖动更新多个元素

如何让 touchesMoved 只控制一个视图?