- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
touchBeganPoint = [touch locationInView:[[UIApplication sharedApplication] keyWindow]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:[[UIApplication sharedApplication] keyWindow]];
CGFloat xOffSet = touchPoint.x - touchBeganPoint.x;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (xOffSet < 0) {
[appDelegate makeRightViewVisible];
}
else if (xOffSet > 0) {
[appDelegate makeLeftViewVisible];
}
self.navigationController.view.frame = CGRectMake(xOffSet,
self.navigationController.view.frame.origin.y,
self.navigationController.view.frame.size.width,
self.navigationController.view.frame.size.height);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// animate to left side
if (self.navigationController.view.frame.origin.x < -kTriggerOffSet)
[self moveToLeftSide];
// animate to right side
else if (self.navigationController.view.frame.origin.x > kTriggerOffSet)
[self moveToRightSide];
// reset
else
[self restoreViewLocation];
}

本文介绍了一个iOS应用中实现滑动切换视图的功能。通过监听触摸开始、移动和结束事件,实现了左右滑动来显示不同视图的效果,并通过AppDelegate进行视图管理。

被折叠的 条评论
为什么被折叠?



