touchesbegan、touchesmoved、touchesend 问题

发布于 2024-12-03 02:27:01 字数 1858 浏览 0 评论 0原文

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

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

https://i.sstatic.net/vdN1T.jpg

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

For various reasons, I've moved these methods from a UIView subclass to my viewcontroller. And I finally got it working, except for one thing. Not only am I able to drag the UIImageviews I've programmatically created, but the actual view controllers view is draggable too. Creating this much undesired effect. I guess it's the fact that it's touches anyobject, and the background itself is an object. I'm just not sure how exclude the background. I would think that it would need the "UserInteraction enabled", but I guess not? I only want it to make UIImageViews draggable. Please forgive my noobness. I'm still learning.

I have all the imageviews i'd want "touchable" in an NSMutableDictionary called "letterDictionary". Would it be possible to only have touch apply to what's in the dictionary?

https://i.sstatic.net/vdN1T.jpg

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫以 2024-12-10 02:27:01

您拥有被触摸的视图,

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

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

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

You have the view that was touched,

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

just test to see if it is the class you are looking for (e.g. a UIImageView) then return

    UITouch *touch = [touches anyObject];
    if (![[touch view] isKindOfClass:[UIImageView class]])
    {
       return;
    }
软糖 2024-12-10 02:27:01

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

In this touchesBegen Method :

-(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 Method

  -(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();}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文