我正在构建显示新闻文章的 iPhone 应用。
为了阅读一篇文章,我有一个带有 UITableView 的 View ,它显示文章的内容。
我需要什么: 我希望用户可以用手指滑动到下一篇文章,效果就像苹果的pageControl示例,或者像在图片之间滑动。(来回移动视觉效果)
问题: 唯一的方法是使用 UIScrollView 和“页面”, 或者有简单的方法包括检测滑动+操纵开关效果+推送 View Controller ?
我还想到的另一个方向是,如果我可以创建一个带有一个水平表格 View 的 View ,每个单元格将显示一篇文章,用户将滑动以移动到下一个单元格。
顺便说一句,iphone 的 fox news 应用程序有这个功能,但他们似乎使用“页面控制”解决方案。
任何信息都会有所帮助。
最佳答案
页面控制不是我认为的唯一解决方案。 想象一下,您必须在 viewController 中查看 View ,然后当您检测到滑动及其方向时, 你所要做的就是:
- 将内容添加到将出现的 View 中
- 将 View 添加为 View Controller 的 subview
- 将该 View 放在正确的一侧(如果用户向右滑动,则放在左侧)
- 最后动画 View 移动(位置)到正确的方向
你就完成了...
这是一个例子:
// in your uiviewcontroller subclass
UIVIew *viewOne, *viewTwo;
-(void)swipeFromView:(UIVIew *)visibleView
toView:(UIView *)pushingView
direction:(UISwipeGestureRecognizerDirection)aDirection {
// assuming the content of pushingView already set
CGPoint visibleViewCenter = [visibleView center]; // register the center
CGRect visibleViewFrame = [visibleView frame];
CGPoint pushingViewCenter, visibleViewNewCenter;
visibleViewNewCenter.y = visibleViewCenter.y;
pushingViewCenter.y = visibleViewCenter.y;
// I use 2 here, but you would make more calculations
pushingViewCenter.x = 2 * visibleViewFrame.width;
visibleViewNewCenter.x = -1 * 2 * visibleViewFrame.width;
// reversing x if direction is right
if(aDirection == UISwipeGestureRecognizerDirectionRight) {
pushingViewCenter.x *= -1;
visibleViewNewCenter.x *= -1;
}
// once the tmp center for pushingView is calculated
// set it, and run the animation
[pushingView setCenter:pushingViewCenter];
[UIVIew animateWithDuration:theDurationYouWant
animations:^{
[visibleView setCenter:visibleViewNewCenter];
[pushingView setCenter:visibleViewCenter];
}];
}
// somewhere else, you handle the swipe
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender {
// set content, etc
[self swipeFromView:viewOne toView:viewTwo direction:[sender direction]];
}
希望这对您有所帮助。
关于iphone - 通过滑动切换 View 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6562016/