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];
}