首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIView触摸问题/问题

UIView触摸问题/问题
EN

Stack Overflow用户
提问于 2011-06-23 18:31:40
回答 2查看 334关注 0票数 0

我的应用程序出了点问题。我在质疑我的做法。

这是一张图片

基本上,我需要彩色轮子在透明的盒子里旋转。到目前为止我所拥有的是起作用的。我可以拖动彩色轮,它就会旋转。问题是,我可以触摸和拖动‘任何地方’的屏幕和它将旋转。我只想让它在“窗口”里旋转。

我基本上添加了一个UIView和一个UIImageView,在ImageView中添加了出口,并在touchesBegan和touchesMoved中添加了代码来执行动画。车轮是一个完整的圆形图像,子视图被“剪裁”为不显示图像的下半部。

代码语言:javascript
复制
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *thisTouch = [touches anyObject];
    delta = [thisTouch locationInView:wheelImage];

    float dx = delta.x  - wheelImage.center.x;
    float dy = delta.y  - wheelImage.center.y;
    deltaAngle = atan2(dy,dx); 

    initialTransform = wheelImage.transform;   
  }

  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
  CGPoint pt = [touch locationInView:wheelImage];

  float dx = pt.x  - wheelImage.center.x;
  float dy = pt.y  - wheelImage.center.y;
  float ang = atan2(dy,dx);

  //do the rotation
  if (deltaAngle == 0.0) {
    deltaAngle = ang;
    initialTransform = wheelImage.transform;
  }else
  {
    float angleDif = deltaAngle - ang;
    CGAffineTransform newTrans = CGAffineTransformRotate(initialTransform, -angleDif);
    wheelImage.transform = newTrans;
    
    currentValue = [self goodDegrees:radiansToDegrees(angleDif)];
    
  }

  }

Now...My问题如下:

如何获得触摸? main /只开始在UIImageView?

  • Should或UIView上工作,我为这个轮子创建了一个自定义UIView,然后将它添加到我的主视图控制器中? 2.a。如果我确实创建了一个自定义,那么我是否也会遇到同样的问题呢?
EN

回答 2

Stack Overflow用户

发布于 2011-06-23 18:37:23

简单的解决方案是在您喜欢的区域开始触摸时添加一个设置为BOOLtrue变量:

代码语言:javascript
复制
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *thisTouch = [touches anyObject];
    CGPoint p = [thisTouch locationInView:wheelImage.superview];
    if (CGRectContainsPoint(wheelImage.frame, p))
    {
      rotating = YES;
      // rest of your code
      ...
    }
}

以及:

代码语言:javascript
复制
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  if (rotating)
  {
     // your code
  }
}

记住在touchesEndedtouchesCanceled中重置touchesCanceled

票数 2
EN

Stack Overflow用户

发布于 2011-06-23 18:36:17

如果子类UIImageView并将该代码放入其中,您将只从该视图中得到触摸,您可能需要将acceptsUserInteraction设置为YES。还将locationInView:改为self;如果要从nib加载,请确保将该类设置为该UIImageView的新子类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6458931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档