2011-09-05 110 views
4

由于各种原因,我已将这些方法从UIView子类移到我的viewcontroller。我终于搞定了,除了一件事。我不仅可以拖动以编程方式创建的UIImageviews,但实际的视图控制器视图也可以拖动。造成这种不良影响。我猜这是触及anyobject的事实,而背景本身就是一个对象。我只是不确定如何排除背景。我会认为它需要“UserInteraction启用”,但我猜不是?我只希望它使UIImageView可拖动。请原谅我的noobness。我还在学习。touchesbegan,touchesmoved,touchesended问题

我有一个名为“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; 
} 

回答

2

您有被感动的观点,

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

只是测试,看它是否是你正在寻找的类(如UIImageView)然后返回

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

,完美的工作!我应该知道这样做!出于好奇,但是,你或者有人告诉我如何将它添加到我的NSMutableDictionary中的对象(UIImageViews)? – Jason

+0

请参阅http://stackoverflow.com/questions/4073598/user-interaction-on-a-uiimageview – cdasher

0

在这个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();} 
相关问题