Reputation: 41
I am creating an app and do not know the best way to go about it.
I want to create a view that has a label and a UIimageview
. I want the user to be able to swipe infinitely left or right like a page application. Each time the user swipes, it swipes to an identical view controller except with a different label and UIimage
that has been called from an array.
The problem is I am not sure the best method to go about this?
Possible Method 1
. Is there a way to use 1 view controller and write code to allow the user to swipe that single view left or right onto the exact same view controller just with updated label and photo?
Possible Method 2
. Could I make two (or possibly three) identical view controllers that the user can infinitely swipe between, for example the user starts on view 1 and swipes right to view 2, then swipes right to view 1 again etc, however each time the user swipes the other view controller will load the next data in the array (or the previous depending on the direction of the swipe)
Can anyone please give me advice on the best way to approach this situation, and what tools/methods/classes/etc I should use to achieve this, for example should i use a "page view controller" or a UIScroll
View etc.
If someone could help me with a guide line on what to do, I can independently go off and research what I need to do! Thanks anyone for any help, I am struggling with this situation!
Notes: The arrays could be anywhere from 10 to 500 and unlikely but possibly 1000 different objects that I will be calling from the Web, so I am not sure the best way to be able to load the objects efficiently and avoid taking up memory and space.
Upvotes: 0
Views: 667
Reputation: 1580
Hi you could try this for your "Possible Method 1" approach
In your "ViewDidLoad" function in your view controller add the following.
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
Then add this function and load the relevant Label / Image values.
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
// Set your Label and Image Values here for RIGHT swipe
...
case UISwipeGestureRecognizerDirection.Left:
// Set your Label and Image Values here for LEFT swipe
...
default:
break
}
}
}
Upvotes: 0
Reputation: 10175
First of all, it seems that you have a lots of data that needs to be displayed or shown to the user. In this case you definitely have to use/create a reuse mechanism in order to prevent memory warnings.
There are several ways to do it:
Use a UIScrollView
and add use the container pattern to add the UIViewController
's view as subview to the UIScrollView
. Major downside with this approach is that you have to create your own reuse mechanism.
Use a UICollectionView
and add also use the container pattern to add the UIViewController
's view as subview in the collection cells. This approach is really good when you display UIViews
, it provides reuse mechanism, but it can be tricky to use it with UIViewController
.
Last choice, and from my point of view, the best one, is to use UIPageViewController, it provides reuse mechanism and it handles UIViewController
subclasses. Also provides cool animations for transitions.
NOTE: Please check the minimum iOS version required in order to use it on iPhone. For iPad min iOS is iOS 5.
Regarding the infinite scroll, you can achieve that by playing with the data source of point 2,3. (Both implementations require a data source)
Upvotes: 1