简体   繁体   English

iPhone编码-如何向触摸事件添加旋转?

[英]iPhone coding - How to add a rotation to the touch event?

Ho can i add a rotation to the object addition to the dragging ? 我可以向拖动对象添加旋转吗?

i dosen't have to be multi touch. 我不必多点触摸。

code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:self.view];
    CGFloat dx = currentTouch.x - carMove.position.x;
    CGFloat dy = currentTouch.y - carMove.position.y;
    CGFloat dist = sqrt(dx * dx + dy * dy);
    if(dist <carMove.radius) {
        carMove.velocity = CGPointMake(0.0, 0.0);
        carMove.dragging = YES;
    }

    lastTouch = currentTouch;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:self.view];
    if( carmove.dragging ){
        carMove.position = currentTouch;
        carMove.velocity = CGPointMake(currentTouch.x - lastTouch.x, currentTouch.y - lastTouch.y);
    }
    lastTouch = currentTouch;
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    carMove.dragging = NO;
}

Try something like 尝试类似

for (int c=0; c < imagesArray.count; c++) {

  MyCustomImageObject img = [imagesArray objectAtIndex:c];

  CGContextSaveGState(context);

  CGContextTranslateCTM(context,
    img.centreOfRotatedImageInBottomLeftPixelCoordsX,
    img.centreOfRotatedImageInBottomLeftPixelCoordsY);

  CGContextRotateCTM(context, img.floatingPointImageAngleInRadians);

  CGContextDrawImage(context,
    CGMakeRect(-img.width/2, -img.height/2, +img.width/2, +img.height/2),
    image.CGImage);

  CGContextRestoreGState(context);
}

Or if you want to have one view per image, use code like that inside the loop once in the drawRect: method of your custom image views. 或者,如果您希望每个图像只有一个视图,请在自定义图像视图的drawRect:方法中一次在循环内使用类似的代码。

EDIT 编辑

You need to register for the accelerometer events if you want to base rotation on phone rotation. 如果要基于手机旋转旋转,则需要注册加速计事件。 You'll need to apply a low-pass filter, like Apple suggest in the docs, in the function that receives the events: 您需要在接收事件的函数中像苹果在文档中建议的那样应用低通滤波器:

#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
  (UIAcceleration *)acceleration {
  // Use a basic low-pass filter to keep only the gravity component of each axis.
  accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
  accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
  accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));
}

Then simply do an atan(accelY,accelX), set it on the object being moved and call setNeedsDisplay. 然后只需执行atan(accelY,accelX),将其设置在要移动的对象上,然后调用setNeedsDisplay。

If the performance isn't high enough, you could try testing the CGRect sent to drawRect and only redrawing images that are within the redraw rectangle;, but beware, if the rectangle has moved, you'll also want to (scrub out/erase) the rectangle it was at just before it moved, so keep a record of those and make a larger repaint rectangle from the minimum and maximum X and Y values in old and new rectangles. 如果性能不够高,则可以尝试测试发送到drawRect的CGRect并仅重绘位于重绘矩形内的图像;但是请注意,如果矩形已移动,则还需要(擦洗/擦除) )移动之前的矩形,因此请记录下来并根据新旧矩形中X和Y值的最小值和最大值创建一个更大的重绘矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM