[英]iPhone coding - How to add a rotation to the touch event?
我可以向拖動對象添加旋轉嗎?
我不必多點觸摸。
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;
}
嘗試類似
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);
}
或者,如果您希望每個圖像只有一個視圖,請在自定義圖像視圖的drawRect:方法中一次在循環內使用類似的代碼。
編輯
如果要基於手機旋轉旋轉,則需要注冊加速計事件。 您需要在接收事件的函數中像蘋果在文檔中建議的那樣應用低通濾波器:
#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));
}
然后只需執行atan(accelY,accelX),將其設置在要移動的對象上,然后調用setNeedsDisplay。
如果性能不夠高,則可以嘗試測試發送到drawRect的CGRect並僅重繪位於重繪矩形內的圖像;但是請注意,如果矩形已移動,則還需要(擦洗/擦除) )移動之前的矩形,因此請記錄下來並根據新舊矩形中X和Y值的最小值和最大值創建一個更大的重繪矩形。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.