UIView 繼承的 UIResponder (負責UI事件處理) 類中提供了四個方法處理多點觸控: spa
touchesBegan方法在觸控開始時被調用,
touchesMoved方法在觸控移動時被調用
touchesEnded方法在觸控結束時被調用
touchesCancelled方法在系統強制結束觸控時被調用 對象
上述方法的參數: 繼承
相關類型說明: 接口
OpenGL ES 程序模板中的多點觸控處理 事件
獲取各類觸控信息的方法
獲取觸控對象(UITouch) ci
UITouch 對象的各類屬性 rem
示例代碼( Metronome by Apple ) it
UITouch *touch = [[event allTouches] anyObject]; io
lastLocation = [touch locationInView:self]; event
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self];
CGFloat xDisplacement = location.x - lastLocation.x;
CGFloat yDisplacement = location.y - lastLocation.y;
CGFloat xDisplacementAbs = fabs(xDisplacement);
CGFloat yDisplacementAbs = fabs(yDisplacement);
// If the displacement is vertical, drag the weight up or down. This will impact the speed of the oscillation.
if ((xDisplacementAbs 1)) {
[self stopSoundAndArm];
[self dragWeightByYDisplacement:yDisplacement];
lastLocation = location;
tempoChangeInProgress = YES;
} else if (xDisplacementAbs >= yDisplacementAbs) {
// If displacement is horizontal, drag arm left or right. This will start oscillation when the touch ends.
CGFloat radians = atan2f(location.y - kArmBaseY, location.x - kArmBaseX);
CGFloat rotation = RadiansToDegrees(radians) + 90.0;
[self rotateArmToDegree:rotation];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self];
CGFloat xDisplacement = location.x - lastLocation.x;
CGFloat yDisplacement = location.y - lastLocation.y;
CGFloat xDisplacementAbs = fabs(xDisplacement);
CGFloat yDisplacementAbs = fabs(yDisplacement);
[self stopSoundAndArm];
if (tempoChangeInProgress) {
[self dragWeightByYDisplacement:yDisplacement];
[self startSoundAndAnimateArmToRight:YES];
tempoChangeInProgress = NO;
} else if (xDisplacementAbs > yDisplacementAbs) {
// horizontal displacement, start oscillation
BOOL startToRight = (xDisplacement >= 0.0) ? YES : NO;
[self startSoundAndAnimateArmToRight:startToRight];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
tempoChangeInProgress = NO;
[self stopSoundAndArm];
}