iOS:滑动手势以在同一控制器中加载不同的数据
Posted
技术标签:
【中文标题】iOS:滑动手势以在同一控制器中加载不同的数据【英文标题】:iOS: swipe gesture to load different data within same controller 【发布时间】:2014-04-18 10:44:16 【问题描述】:在我的应用程序中,我正在尝试实现一个功能(类似于在同一视图中月份更改的日历),例如如果当前视图显示当天(今天)的新闻,那么如果用户向右滑动,它应该显示下一个每日新闻,向左滑动应显示前一日新闻。
I have so far
在表格视图中显示今日新闻的单个 NewsViewController
在其他主题上,许多人建议使用类似这个问题的库 Tom 建议 Handling a view controller that slides in with either way swipe 我是ios开发新手,不知道怎么做,任何帮助将不胜感激。
【问题讨论】:
查看此链接developer.apple.com/library/ios/samplecode/… 【参考方案1】:所以你可以在这里https://***.com/a/20596933/1307844https://***.com/a/20596933/1307844尝试使用任何手势方法提取数据
你的答案完全是你如何实现你的手势方法。
【讨论】:
感谢您的回复,但如何在滑动时显示具有不同数据的相同 viewController。 意味着相同的视图将迭代新的 Web 服务。请问您有任何在线教程链接吗?这说明了如何做到这一点? @MuhammadUmair :有很多可能性取决于您的实现。对于一般情况,假设您正在通过服务调用在UILabel
中显示您的新闻数据。因此,如果您向左滑动,请加载第二天数据的服务调用。如果手势是向右滑动,请使用服务调用获取前一天的数据。我相信,您一定知道如何调用服务或阅读提要。最好将问题分解成小块,例如服务调用、数据表示、手势,然后重新加入所有小块。【参考方案2】:
我找到了解决方案,这是我的代码 sn-ps...
ViewDidLoad:
UISwipeGestureRecognizer *oneFingerSwipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(oneFingerSwipeLeft:)];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[viewContent addGestureRecognizer:oneFingerSwipeLeft];
UISwipeGestureRecognizer *oneFingerSwipeRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(oneFingerSwipeRight:)];
[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[viewContent addGestureRecognizer:oneFingerSwipeRight];
oneFingerSwipeLeft:
- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer
newsList = [[NSMutableArray alloc] init];
//to animate the view as new view is loaded
[UIView animateWithDuration:0.1 animations:^
viewContent.frame = CGRectMake( -viewContent.frame.size.width, viewContent.frame.origin.y , viewContent.frame.size.width, viewContent.frame.size.height);
[self loadData];
completion:^(BOOL finished)
viewContent.frame = CGRectMake( viewContent.frame.size.width,viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);
[UIView animateWithDuration:0.3 animations:^
viewContent.frame = CGRectMake(0.0, viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);
];
];
selectedDay++;
[self fetchDataFromWeb];
oneFingerSwipeRight:
- (void)oneFingerSwipeRight:(UITapGestureRecognizer *)recognizer
newsList = [[NSMutableArray alloc] init];
//to animate the view as new view is loaded
[UIView animateWithDuration:0.1 animations:^
viewContent.frame = CGRectMake( viewContent.frame.size.width, viewContent.frame.origin.y , viewContent.frame.size.width, viewContent.frame.size.height);
[self loadData];
completion:^(BOOL finished)
viewContent.frame = CGRectMake( -viewContent.frame.size.width,viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);
[UIView animateWithDuration:0.3 animations:^
viewContent.frame = CGRectMake(0.0, viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);
];
];
selectedDay--;
[self fetchDataFromWeb];
感谢@BalramTiwari 的宝贵建议。
【讨论】:
【参考方案3】:这是 IceFish 的快速回答:
override func viewDidLoad()
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
和响应者:
func respondToSwipeGesture(gesture: UIGestureRecognizer)
if let swipeGesture = gesture as? UISwipeGestureRecognizer
switch swipeGesture.direction
case UISwipeGestureRecognizerDirection.Left:
UIView.animateWithDuration(0.1, animations:
let myFrame = self.view.frame
self.view.frame = CGRectMake(-myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
, completion: _ in
let myFrame = self.view.frame
self.view.frame = CGRectMake(myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
UIView.animateWithDuration(0.3, animations:
let myFrame = self.view.frame
self.view.frame = CGRectMake(0.0, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
, completion: nil)
)
// do left action here
case UISwipeGestureRecognizerDirection.Right:
UIView.animateWithDuration(0.1, animations:
let myFrame = self.view.frame
self.view.frame = CGRectMake(myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
, completion: _ in
let myFrame = self.view.frame
self.view.frame = CGRectMake(-myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
UIView.animateWithDuration(0.3, animations:
let myFrame = self.view.frame
self.view.frame = CGRectMake(0.0, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
, completion: nil)
)
// do right action here
default:
break
【讨论】:
以上是关于iOS:滑动手势以在同一控制器中加载不同的数据的主要内容,如果未能解决你的问题,请参考以下文章
iOS8 UISplitViewController 始终在滑动手势上打开主控制器