Change label text while image in UIScrollview changes - ios

I want to create an image gallery similar to this:
http://sweettutos.com/2015/04/13/how-to-make-a-horizontal-paging-uiscrollview-with-auto-layout-in-storyboards-swift/
Where each time the user swipes and the image changes and the label title changes.I have written the code and included the if statement, however, it does not work properly and it only changes as one value.
Here is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mainscrollview: UIScrollView!
var imageArray = [UIImage]()
#IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
mainscrollview.frame = view.frame
imageArray = [#imageLiteral(resourceName: "goku"), #imageLiteral(resourceName: "boruto"), #imageLiteral(resourceName: "tail"), #imageLiteral(resourceName: "sage"),#imageLiteral(resourceName: "tobi")]
for i in 0..<imageArray.count{
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainscrollview.frame.width,height: self.mainscrollview.frame.height)
mainscrollview.contentSize.width = mainscrollview.frame.width * CGFloat(i + 1)
mainscrollview.addSubview(imageView)
let gokuImage = UIImage(named: "goku")
let borutoImage = UIImage(named: "boruto")
if imageView.image == gokuImage{
label.text = "Goku"
}
if imageView.image == borutoImage{
label.text = "bor"
}
}
}
}
This is where the project is going wrong:
let gokuImage = UIImage(named: "Goku")
let borutoImage = UIImage(named: "boruto")
if imageView.image == gokuImage{
label.text = "Goku"
}
if imageView.image == borutoImage{
label.text = "bor"
}
I think this is because the if statement can not directly access the current image that is being displayed in the UIScroll view.
Here is the link to my project: http://www.mediafire.com/file/adkzcpvdtodlyl5/gallery_copy.zip

To achieve what you want to do, here is one of the option available which is fairly simple:
Your label is in your ScrollView, so it will scroll with your first image. You need to move it above your ScrollView, at the same hierarchy level as the ScrollView.
In order to change your label's text when you scroll, you can detect when your page has stopped decelerating. To do so:
(a) Add UIScrollViewDelegate to class ViewController: UIViewController,
(b) in viewDidLoad(), add 'mainscrollview.delegate = self',
(c) write the 'scrollViewDidEndDecelerating' method.
In the end, your code should look like this:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var mainscrollview: UIScrollView!
#IBOutlet var label: UILabel!
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
imageArray = [#imageLiteral(resourceName: "goku"), #imageLiteral(resourceName: "boruto"), #imageLiteral(resourceName: "tail"), #imageLiteral(resourceName: "sage"),#imageLiteral(resourceName: "tobi")]
mainscrollview.frame = view.frame
mainscrollview.delegate = self
label.text = "Initial label value is Boruto"
for i in 0..<imageArray.count{
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainscrollview.frame.width,height: self.mainscrollview.frame.height)
mainscrollview.contentSize.width = mainscrollview.frame.width * CGFloat(i + 1)
mainscrollview.addSubview(imageView)
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
// Test the offset and calculate the current page after scrolling ends
let pageWidth:CGFloat = mainscrollview.frame.width
let currentPage:CGFloat = floor((mainscrollview.contentOffset.x-pageWidth/2)/pageWidth)+1
// Change the text accordingly
if Int(currentPage) == 0{
label.text = "Boruto"
}else if Int(currentPage) == 1{
label.text = "Goku"
}else if Int(currentPage) == 2{
label.text = "Sage"
}else if Int(currentPage) == 3{
label.text = "Tail"
}else if Int(currentPage) == 4{
label.text = "Tobi"
}else{
label.text = "Other character"
}
}
}

The first thing you should do is remove the label from the scroll view and add it into the main view i.e the scrollview and the label should have sibling relationship rather than parent-child relationship.
Modify your ViewController accordingly
class ViewController: UIViewController, UIScrollViewDelegate {
// your other code
override func viewDidLoad() {
super.viewDidLoad()
mainscrollview.delegate = self
// your other code
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let imageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
switch imageNumber {
case 0:
label.text = "Goku"
case 1:
label.text = "Bor"
case 2:
label.text = "Tail"
case 3:
label.text = "Sage"
case 4:
label.text = "Tobi"
default:
label.text = "Unknown"
}
}
}

You use the UILabel from storyboards and UIImageView from the code. You add all the images to the scrollView, so that everyone follows one another, but you use your label only with the first image.
In other words, you need to add UILabel to the code and add it to each image. Instead of a lot of "if-cycle", I suggest you use an array of labels, just as you use the image.
var labelArray = [String]()
// in viewDidLoad add your label names
labelArray = ["goku", "boruto", "next word", "next word", "next word"]
// create UILabel
let yourLabel = UILabel(frame: CGRect(x: xPosition, y: self.mainscrollview.frame.height - 100, width: self.mainscrollview.frame.width, height: 40))
// add each labelName to each image
yourLabel.text = labelArray[i]
// add image and label to scrollView
mainscrollview.addSubview(imageView)
mainscrollview.addSubview(yourLabel)

Related

UIView.animate with imageArray inside of UIScrollView but with effect like paging enable

I'm new in Swift but I have some basic experience.
I have successfully created an animation with three images and they repeat themselves. But I would like the effect of this repetition to be like when you in attributes inspector of UIScrollView check paging enable.
Inside of options: UIView.AnimationOptions I tried different Constants but I can not find that suits me.
My question: Can I animate image array like you swipe images in scroll view with paging enable?
class ViewController: UIViewController {
#IBOutlet weak var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
imageArray = [UIImage(named: "forest")!, UIImage(named: "slika1")!, UIImage(named: "slika2")!]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.miniView.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
startAnimating()
}
func startAnimating() {
var newOffset = mainScrollView.contentOffset
newOffset.x = 0.0
newOffset.x += mainScrollView.frame.width * CGFloat(imageArray.count - 1)
UIView.animate(withDuration: Double(imageArray.count), delay: 5, options: [.repeat, .allowUserInteraction], animations: {
self.mainScrollView.contentOffset = newOffset
})
}
}
Ok, if i correctly understand what you want, this is the correct answer.
Animate page by page, with a 5 seconds delay with one animation and another.
You need to use Timer.scheduledTimer to trigger method that scroll to the next page. With that solution, you can check paging enable ON, so the user can change the page itself.
I hope it helps
class ViewController: UIViewController {
#IBOutlet weak var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
var currentPage = 0
override func viewDidLoad() {
super.viewDidLoad()
imageArray = [UIImage(named: "forest")!, UIImage(named: "slika1")!, UIImage(named: "slika2")!]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.miniView.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(startAnimating), userInfo: nil, repeats: true)
}
#objc func startAnimating() {
//that line check if the current page is the last one, and set it to the first, otherwise, it increment the currentPage
self.currentPage = (self.currentPage == imageArray.count-1) ? 0 : self.currentPage+1
var newOffset = mainScrollView.contentOffset
newOffset.x = mainScrollView.frame.width * CGFloat(self.currentPage)
self.mainScrollView.setContentOffset(newOffset, animated: true)
}
}

Delete image out of array at given index

I have created and initialized a scrollview that pulls from an array of images. So a user swipes past a photo, then another, and I need to delete the image based on the location of the current image I have created a for loop that deletes the image i-- in the array, but this crashes because I can't delete the i. Is there a way I can accomplish this? Thank you: here is my code
class ViewController: UIViewController {
var imageArray = [UIImage]()
#IBOutlet weak var mainScrollView: UIScrollView!
func scrollViewDidScroll(_ scrollView: UIScrollView) {
for i in 0..<imageArray.count {
if i >= 3 {
imageArray.remove(at: i - 2)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.frame = view.frame
imageArray = [#imageLiteral(resourceName: "dubai6"), #imageLiteral(resourceName: "dubai2"), #imageLiteral(resourceName: "dubai7"), #imageLiteral(resourceName: "dubai4"), #imageLiteral(resourceName: "dubai3"), #imageLiteral(resourceName: "dubai5"), #imageLiteral(resourceName: "DigitalDrawingPreview"), #imageLiteral(resourceName: "denarus"), #imageLiteral(resourceName: "dubai1")]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = imageArray[i]
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.view.frame.width, height: self.mainScrollView.frame.height)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
Rather than using a UIScrollView, I would recommend using a UITableView or UICollectionView. It is very easy to manage the data model and then call table.reloadData().
You would track the scroll position with the ScrollViewDelegate method scrollViewDidScroll(_ scrollView) and force a reload once you've scrolled far enough.

Paging UIScrollView seems to place content off centre

I have a UIScrollView that I want to have paging functionality (think an initial splash screen). I want that content (a UILabel and a UIImageView) to be placed centrally in each paging view on the scrollView. My problem is is that it is always slightly off centre ().
Here is the complete code:
var splashScreenObjects = [SplashScreenObject]()
var imageViewArray = [UIImageView]()
var subtitleViewArray = [UILabel]()
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
createSplashScreenObjects()
configurePageControl()
configureScrollView()
}
func createSplashScreenObjects() {
let firstScreen: SplashScreenObject = SplashScreenObject(subtitle: "Medication reminders on your phone. Never miss your next dose", image: UIImage(named: "splashScreen1")!)
let secondScreen: SplashScreenObject = SplashScreenObject(subtitle: "Track how good you have been with your medication", image: UIImage(named: "splashScreen2")!)
let thirdScreen: SplashScreenObject = SplashScreenObject(subtitle: "The better you are with your medication, the more points you'll earn!", image: UIImage(named: "splashScreen3")!)
splashScreenObjects.append(firstScreen)
splashScreenObjects.append(secondScreen)
splashScreenObjects.append(thirdScreen)
}
func configureScrollView() {
self.scrollView.layoutIfNeeded()
self.scrollView.showsHorizontalScrollIndicator = false
self.scrollView.showsVerticalScrollIndicator = false
self.scrollView.pagingEnabled = true
self.scrollView.delegate = self
let width = view.frame.size.width
for index in 0..<splashScreenObjects.count {
let subtitle = UILabel(frame: CGRectMake((width * CGFloat(index)) + 25, self.scrollView.frame.size.height-75, width-50, 75))
subtitle.text = splashScreenObjects[index].subtitle
subtitle.textAlignment = NSTextAlignment.Center
subtitle.textColor = UIColor.whiteColor()
subtitle.font = UIFont(name:"Ubuntu", size: 16)
subtitle.numberOfLines = 2
subtitle.backgroundColor = UIColor.clearColor()
self.scrollView.addSubview(subtitle)
self.subtitleViewArray.append(subtitle)
subtitle.alpha = 0
let mainImage = UIImageView(frame: CGRectMake((width * CGFloat(index)), 50, width, self.scrollView.frame.size.height-150))
mainImage.image = splashScreenObjects[index].image
mainImage.contentMode = UIViewContentMode.ScaleAspectFit
self.scrollView.addSubview(mainImage)
self.imageViewArray.append(mainImage)
mainImage.alpha = 0
}
self.scrollView.contentSize = CGSizeMake(width * CGFloat(splashScreenObjects.count), self.scrollView.frame.size.height-50)
animateViews(Int(0))
}
func configurePageControl() {
self.pageControl.numberOfPages = splashScreenObjects.count
self.pageControl.currentPage = 0
self.view.addSubview(pageControl)
pageControl.addTarget(self, action: #selector(SplashViewController.changePage(_:)), forControlEvents: UIControlEvents.ValueChanged)
}
func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * self.view.frame.size.width
scrollView.setContentOffset(CGPointMake(x, 0), animated: true)
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / self.view.frame.size.width)
pageControl.currentPage = Int(pageNumber)
animateViews(Int(pageNumber))
}
func animateViews(pageNumber: Int) {
UIView.animateWithDuration(0.5, animations: {
self.imageViewArray[pageNumber].alpha = 1.0
self.subtitleViewArray[pageNumber].alpha = 1.0
})
}
Here are my auto layout constraints for the UIScrollView:
Your leading and trailing spaces are both -20, which means that the scroll view is 40 points wider than its superview. Change these to 0.
You should replace
self.scrollView.layoutIfNeeded()
to
self.view.layoutIfNeeded()
because layoutIfNeeded layout caller subviews, not itself. So, scrollView, when you add subtitle and mainImage on it, has wrong frame.

Infinite loop scrolling using UIScrollView in Swift

I am trying to make an infinite loop image gallery app. I did it by setting up a UIScrollView inside which I inserted UIImageViews.
I am able to load 4 images and swipe between them, but the problem I have is how to implement the infinite scroll, so that after the last image, the first one appears and opposite.
As I am newbie in Swift, I don't know how to handle this problem nor where to start. I will appreciate any suggestion, example or link on how to solve this problem.
Thanks in advance!
Here is the peace of my code:
class FirstViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
let scrollViewWidth: CGFloat = self.scrollView.frame.width
let scrollViewHeight: CGFloat = self.scrollView.frame.height
let numPics: Int = 3
let imgOne = UIImageView()
let imgTwo = UIImageView()
let imgThree = UIImageView()
let imgFour = UIImageView()
var arrPics = [imgOne, imgTwo, imgThree, imgFour]
var arrSlide = ["slide1.png", "slide2.png", "slide3.png", "slide4.png"]
for i in 0...numPics {
arrPics[i] = UIImageView(frame: CGRectMake(0,scrollViewHeight * CGFloat(i),scrollViewWidth, scrollViewHeight))
arrPics[i].image = UIImage(named: arrSlide[i])
self.scrollView.addSubview(arrPics[i])
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.width, self.scrollView.frame.height*4)
}
Don't use a scroll view but either a table view or a collection view.
The delegates of both classes want a cell count from you. Then give the delegates an insane number of cells, a number so high that it is totally unlikely that any human being will ever scroll to reach the end. Something like 9999999 cells. This is no problem for such classes because in the background implementation they do not create really this high number of cells but only maximal the number of cells which could be visible at the same time on the screen. The cells in fact are reused. So when cells are falling out at the bottom those cells are going to be reused at the top and vice versa. As a last point is: set the starting point in the middle of such a high number, something like 5555555. I think that is the easiest solution if you are not going to implement a full image recycling algorithm like they did.
#IBOutlet weak var scrollView: UIScrollView!
var scrollViewHeight: CGFloat!
let numPics: Int = 4
override func viewDidLoad()
{
super.viewDidLoad()
scrollView.delegate = self
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
let scrollViewWidth: CGFloat = self.scrollView.frame.width
scrollViewHeight = self.scrollView.frame.height
let imgOne = UIImageView()
let imgTwo = UIImageView()
let imgThree = UIImageView()
let imgFour = UIImageView()
let imgFive = UIImageView()
var arrPics = [imgOne, imgTwo, imgThree, imgFour,imgFive]
var arrSlide = ["slide1.jpeg", "slide1.jpeg", "slide1.jpeg", "slide1.jpeg","slide1.jpeg"]
for i in 0...numPics {
arrPics[i] = UIImageView(frame: CGRectMake(0,scrollViewHeight * CGFloat(i),scrollViewWidth, scrollViewHeight))
arrPics[i].image = UIImage(named: arrSlide[i])
self.scrollView.addSubview(arrPics[i])
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.width, self.scrollView.frame.height * CGFloat(numPics+1))
}
func scrollViewDidScroll(scrollView: UIScrollView)
{
scrollViewHeight = self.scrollView.frame.height
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.width, self.scrollView.frame.height * scrollViewHeight )
}
Using Timer you can use infinite scrolling like below
func showview() {
if(videosimageslider.isEmpty) {
// print("Empty Array")
} else {
// print("Not Empty ")
// print(videosimageslider.count)
for var index=0; index<videosimageslider.count; index++ {
if let url = NSURL(string: videosimageslider[index]) {
if let data = NSData(contentsOfURL: url){
if let imageUrl = UIImage(data: data) {
let myimageview:UIImageView = UIImageView()
// myimageview.image = imageUrl
let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageUrl: NSURL!) -> Void in
//println(self)
}
// let url = NSURL(string: "http://yikaobang-test.u.qiniudn.com/FnZTPYbldNXZi7cQ5EJHmKkRDTkj")
myimageview.sd_setImageWithURL(url, completed: block)
myimageview.frame.size.width = imagewidth
myimageview.frame.size.height = imageheight
myimageview.contentMode = UIViewContentMode.ScaleToFill
myimageview.frame.origin.x = xscrollpos
myimageview.frame.origin.y = 5
scrollview!.addSubview(myimageview)
xscrollpos += imagewidth
scrollviewcontentSize += imagewidth
scrollview.contentSize = CGSize(width: scrollviewcontentSize, height: imageheight)
}
}
}
}
scrollingTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "newStartScrolling", userInfo: nil, repeats: true)
scrollingTimer.fire()
}
}
// call the timer function //
func newStartScrolling() {
var currentOffset = scrollview.contentOffset
currentOffset = CGPointMake(currentOffset.x+3, currentOffset.y )
if(currentOffset.x < scrollview.contentSize.width - 500) {
scrollview.setContentOffset(currentOffset, animated: false)
currentOffset = CGPointMake(0, currentOffset.y )
//scrollview.contentSize = CGSize(width: 0, height: 0);
} else {
scrollingTimer.invalidate()
showview()
}
}
This is what will solve your problem, just adjust the controller to go in vertical slide view and load the resources at your will.
Link

Swift Image gallery with fullscreen slider

I want to build a facebook-like image gallery but don't know where to start from.
Here's what i think i should do:
CollectionView that shows thumbnails based on an array
On row-click, i modally open a pageViewController based on the same array of images
On Swipe left-right, i go to the next or previous image
On Swipe up-down, i close this view and go back to my collectionview
Is this logic correct/the best practice?
Maybe it's been already done by someone.
You can use banana library for showing images in slider view in Swift
get banana from https://github.com/gauravkatoch007/banana
import banana
#IBOutlet weak var imageScrollView: UIScrollView!
// Here imageArray can be a string array of Image URLs
var imageArray = [String]()
//or imageArray can be a array of UIImages
var imageArray = [UIImage]()
var imageScroll = banana( imageScrollView :self.imageScrollView )
//Load to load images in memory and display them in User Interface
imageScroll!.load(imageArray)
//Call startScroll for autoScrolling. Default scrolling timer is 8 seconds
imageScroll!.startScroll()
//Call this function to stop autoScrolling on touch or swipe.
imageScroll!.assignTouchGesture()
Update I resolved in this way:
Gallery is a collection view
On cell selected, i modally segue to another view, which contains a scrollview in which i put 5 images one (right) after the other, and handle paginating.
My image will always be the third in the middle, after every scroll, i choose the 5 images of my entire array which centers the index, or the 5 which contain this.
here's my class PhotoViewController: UIViewController, UIScrollViewDelegate {
var screenSize: CGRect = UIScreen.mainScreen().bounds
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var blackView: UIView!
#IBOutlet weak var doneButton: UIButton!
#IBOutlet weak var photoActions: UIImageView!
var img1:UIImageView = UIImageView(image: UIImage())
var img2:UIImageView = UIImageView(image: UIImage())
var img3:UIImageView = UIImageView(image: UIImage())
var img4:UIImageView = UIImageView(image: UIImage())
var img5:UIImageView = UIImageView(image: UIImage())
var w1: UIImage!
var w2: UIImage!
var w3: UIImage!
var w4: UIImage!
var w5: UIImage!
var pageIndex: Int!
var currentPage: Int!
var oldPage: Int!
var endFrame: CGRect!
var arrayIndex: Int!
// placeholder for if the image frame is scrolled
var scrolledPhotoFrame: CGRect!
override func viewDidLoad() {
super.viewDidLoad()
RightSize()
LoadData()
}
func LoadData(){
// data passed from feedViewController gets stored here
img1.image = w1
img2.image = w2
img3.image = w3
img4.image = w4
img5.image = w5
// set the right endFrame based on the selectedImage
switch pageIndex {
case 0:
img1.frame = endFrame
img1.frame.origin.x = 0
case 1:
img2.frame = endFrame
img2.frame.origin.x = screenSize.width
case 2:
img3.frame = endFrame
img3.frame.origin.x = (screenSize.width * 2)
case 3:
img4.frame = endFrame
img4.frame.origin.x = (screenSize.width * 3)
case 4:
img5.frame = endFrame
img5.frame.origin.x = (screenSize.width * 4)
default:
break
}
currentPage = pageIndex
oldPage = pageIndex
scrollView.contentSize = CGSize(width: screenSize.width*5, height: screenSize.height)
scrollView.contentOffset.x = CGFloat(pageIndex * Int(screenSize.width))
println(scrollView.contentOffset.x)
// default value of scrolledPhotoFrame is unscrolled position of photoImageView
scrolledPhotoFrame = endFrame
// required for registering scroll events
scrollView.delegate = self
// RightSize()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func doneDidPress(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
// called while scrolling
func scrollViewDidScroll(scrollView: UIScrollView) {
currentPage = Int(scrollView.contentOffset.x / screenSize.width)
if (currentPage != oldPage){
//Ho cambiato pagina
if (currentPage>oldPage){
//Sono andato di 1 foto avanti
println("Avanti")
arrayIndex = arrayIndex+1
CambioPagina()
}else{
//Sono andato di 1 foto indietro
arrayIndex = arrayIndex-1
println("indietro")
}
oldPage=currentPage
}
var alpha = CGFloat(1 - abs(scrollView.contentOffset.y) / 100)
var alpha2 = CGFloat(1 - abs(scrollView.contentOffset.y) / 20)
blackView.alpha = alpha
doneButton.alpha = alpha2
photoActions.alpha = alpha2
}
// This method is called right as the user lifts their finger
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
var offsetY = scrollView.contentOffset.y
var alpha = CGFloat(1 - abs(scrollView.contentOffset.y) / 240)
if (abs(offsetY) > 100) {
scrolledPhotoFrame.origin.y = scrolledPhotoFrame.origin.y - offsetY
blackView.hidden = true
scrollView.hidden = true // could be wrong
doneButton.hidden = true
dismissViewControllerAnimated(true, completion: nil)
}
}
// selecting the view to zoom
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
switch currentPage {
case 0:
return img1
case 1:
return img2
case 2:
return img3
case 3:
return img4
case 4:
return img5
default:
return nil
}
}
func scrollViewWillBeginZooming(scrollView: UIScrollView, withView view: UIView!) {
img2.hidden = true
img3.hidden = true
img4.hidden = true
img5.hidden = true
}
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView!, atScale scale: CGFloat) {
}
//Controllo per cambio pagina..! chissa!
func CambioPagina(){
switch arrayIndex{
case 0:
w1 = UIImage(named: arrayFoto[arrayIndex])
w2 = UIImage(named: arrayFoto[arrayIndex+1])
w3 = UIImage(named: arrayFoto[arrayIndex+2])
w4 = UIImage(named: arrayFoto[arrayIndex+3])
w5 = UIImage(named: arrayFoto[arrayIndex+4])
pageIndex = 0
case 1:
w1 = UIImage(named: arrayFoto[arrayIndex-1])
w2 = UIImage(named: arrayFoto[arrayIndex])
w3 = UIImage(named: arrayFoto[arrayIndex+1])
w4 = UIImage(named: arrayFoto[arrayIndex+2])
w5 = UIImage(named: arrayFoto[arrayIndex+3])
pageIndex = 1
case (arrayFoto.count-2):
w1 = UIImage(named: arrayFoto[arrayIndex-3])
w2 = UIImage(named: arrayFoto[arrayIndex-2])
w3 = UIImage(named: arrayFoto[arrayIndex-1])
w4 = UIImage(named: arrayFoto[arrayIndex])
w5 = UIImage(named: arrayFoto[arrayIndex+1])
pageIndex = 3
case (arrayFoto.count-1):
w1 = UIImage(named: arrayFoto[arrayIndex-4])
w2 = UIImage(named: arrayFoto[arrayIndex-3])
w3 = UIImage(named: arrayFoto[arrayIndex-2])
w4 = UIImage(named: arrayFoto[arrayIndex-1])
w5 = UIImage(named: arrayFoto[arrayIndex])
pageIndex = 4
default:
w1 = UIImage(named: arrayFoto[arrayIndex-2])
w2 = UIImage(named: arrayFoto[arrayIndex-1])
w3 = UIImage(named: arrayFoto[arrayIndex])
w4 = UIImage(named: arrayFoto[arrayIndex+1])
w5 = UIImage(named: arrayFoto[arrayIndex+2])
pageIndex = 2
}
LoadData()
}
func RightSize(){
img1.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height)
scrollView.addSubview(img1)
img2.frame = CGRect(x: screenSize.width, y: 0, width: screenSize.width, height: screenSize.height)
scrollView.addSubview(img2)
img3.frame = CGRect(x: (screenSize.width*2), y: 0, width: screenSize.width, height: screenSize.height)
scrollView.addSubview(img3)
img4.frame = CGRect(x: (screenSize.width*3), y: 0, width: screenSize.width, height: screenSize.height)
scrollView.addSubview(img4)
img5.frame = CGRect(x: (screenSize.width*4), y: 0, width: screenSize.width, height: screenSize.height)
scrollView.addSubview(img5)
img1.contentMode = .ScaleAspectFit
img2.contentMode = .ScaleAspectFit
img3.contentMode = .ScaleAspectFit
img4.contentMode = .ScaleAspectFit
img5.contentMode = .ScaleAspectFit
}
Any better approach will be great, for example, i cannot handle image zoom

Resources