Question

In my application I'm trying to implement a functionality (similar to calendar where month changes within same view) something like if current view shows the news of current day (today) then if user swipe right, it should show the next day news and on swipe left it should show previous day news.

I have so far

a single NewsViewController which shows the today news in table view

On other topics many suggest to use libraries like on this question Tom suggests Handling a view controller that slides in with either way swipe I am new to IOS development and have no idea how to do it, any help will be greatly appreciated.

Was it helpful?

Solution 2

I found the solution and here is my code snippets...

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

}

Thanks to @BalramTiwari for valuable suggestion.

OTHER TIPS

So you can try pulling data in any of the gesture methods using this cod here https://stackoverflow.com/a/20596933/1307844

Your answer is totally how you implement your gesture methods.

Here is IceFish's answer in swift:

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)

and the responder:

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
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top