我的应用程序出了点问题。我在质疑我的做法。
这是一张图片

基本上,我需要彩色轮子在透明的盒子里旋转。到目前为止我所拥有的是起作用的。我可以拖动彩色轮,它就会旋转。问题是,我可以触摸和拖动‘任何地方’的屏幕和它将旋转。我只想让它在“窗口”里旋转。
我基本上添加了一个UIView和一个UIImageView,在ImageView中添加了出口,并在touchesBegan和touchesMoved中添加了代码来执行动画。车轮是一个完整的圆形图像,子视图被“剪裁”为不显示图像的下半部。
- (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?
发布于 2011-06-23 18:37:23
简单的解决方案是在您喜欢的区域开始触摸时添加一个设置为BOOL的true变量:
- (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
...
}
}以及:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (rotating)
{
// your code
}
}记住在touchesEnded和touchesCanceled中重置touchesCanceled。
发布于 2011-06-23 18:36:17
如果子类UIImageView并将该代码放入其中,您将只从该视图中得到触摸,您可能需要将acceptsUserInteraction设置为YES。还将locationInView:改为self;如果要从nib加载,请确保将该类设置为该UIImageView的新子类。
https://stackoverflow.com/questions/6458931
复制相似问题