Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift example update #196

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Example-Swift/ExamplePhoto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ class ExamplePhoto: NSObject, NYTPhoto {
var image: UIImage?
var imageData: NSData?
var placeholderImage: UIImage?
let attributedCaptionTitle: NSAttributedString?
let attributedCaptionSummary: NSAttributedString? = NSAttributedString(string: "summary string", attributes: [NSForegroundColorAttributeName: UIColor.grayColor()])
let attributedCaptionCredit: NSAttributedString? = NSAttributedString(string: "credit", attributes: [NSForegroundColorAttributeName: UIColor.darkGrayColor()])
var attributedCaptionTitle: NSAttributedString?
var attributedCaptionSummary: NSAttributedString? = NSAttributedString(string: "summary string", attributes: [NSForegroundColorAttributeName: UIColor.grayColor()])
var attributedCaptionCredit: NSAttributedString? = NSAttributedString(string: "credit", attributes: [NSForegroundColorAttributeName: UIColor.darkGrayColor()])

init(image: UIImage? = nil, imageData: NSData? = nil, attributedCaptionTitle: NSAttributedString) {
self.image = image
self.imageData = imageData
self.attributedCaptionTitle = attributedCaptionTitle
super.init()
}

override init() {
self.attributedCaptionTitle = nil
super.init()
}

}
78 changes: 60 additions & 18 deletions Example-Swift/PhotosProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,78 @@ import UIKit
import NYTPhotoViewer

/**
* In Swift 1.2, the following file level constants can be moved inside the class for better encapsulation
*/
let CustomEverythingPhotoIndex = 1, DefaultLoadingSpinnerPhotoIndex = 3, NoReferenceViewPhotoIndex = 4
let PrimaryImageName = "NYTimesBuilding"
let PlaceholderImageName = "NYTimesBuildingPlaceholder"
* Struct for Global constants
*/
struct Constants {
static let PrimaryImageName = "NYTimesBuilding"
static let PlaceholderImageName = "NYTimesBuildingPlaceholder"
}

/**
* Photo Index types for different pictures in our data source.
*/
enum ViewControllerPhotoIndex : Int {
case CustomEverything = 1
case LongCaption = 2
case DefaultLoadingSpinner = 3
case NoReferenceView = 4
case CustomMaxZoomScale = 5
case PhotoIndexGif = 6
}

class PhotosProvider: NSObject {

let photos: [ExamplePhoto] = {

var mutablePhotos: [ExamplePhoto] = []
var image = UIImage(named: PrimaryImageName)
let NumberOfPhotos = 5

func shouldSetImageOnIndex(photoIndex: Int) -> Bool {
return photoIndex != CustomEverythingPhotoIndex && photoIndex != DefaultLoadingSpinnerPhotoIndex
}

var image = UIImage(named: Constants.PrimaryImageName)
let NumberOfPhotos = 7

for photoIndex in 0 ..< NumberOfPhotos {
let title = NSAttributedString(string: "\(photoIndex + 1)", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])

let photo = shouldSetImageOnIndex(photoIndex) ? ExamplePhoto(image: image, attributedCaptionTitle: title) : ExamplePhoto(attributedCaptionTitle: title)

if photoIndex == CustomEverythingPhotoIndex {
photo.placeholderImage = UIImage(named: PlaceholderImageName)
let photo = ExamplePhoto()
var caption = "Summary"
switch(photoIndex)
{

case ViewControllerPhotoIndex.CustomEverything.rawValue:
photo.image = nil
photo.placeholderImage = UIImage(named: "NYTimesBuilding")
caption = "photo with custom everything";


case ViewControllerPhotoIndex.LongCaption.rawValue:
caption = "photo with long caption. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum maximus laoreet vehicula. Maecenas elit quam, pellentesque at tempor vel, tempus non sem. Vestibulum ut aliquam elit. Vivamus rhoncus sapien turpis, at feugiat augue luctus id. Nulla mi urna, viverra sed augue malesuada, bibendum bibendum massa. Cras urna nibh, lacinia vitae feugiat eu, consectetur a tellus. Morbi venenatis nunc sit amet varius pretium. Duis eget sem nec nulla lobortis finibus. Nullam pulvinar gravida est eget tristique. Curabitur faucibus nisl eu diam ullamcorper, at pharetra eros dictum. Suspendisse nibh urna, ultrices a augue a, euismod mattis felis. Ut varius tortor ac efficitur pellentesque. Mauris sit amet rhoncus dolor. Proin vel porttitor mi. Pellentesque lobortis interdum turpis, vitae tincidunt purus vestibulum vel. Phasellus tincidunt vel mi sit amet congue.";

case ViewControllerPhotoIndex.DefaultLoadingSpinner.rawValue:
photo.image = nil
caption = "photo with loading spinner";

case ViewControllerPhotoIndex.NoReferenceView.rawValue:
photo.image = nil
caption = "photo without reference view";

case ViewControllerPhotoIndex.CustomMaxZoomScale.rawValue:
photo.image = nil
caption = "photo with custom maximum zoom scale";

case ViewControllerPhotoIndex.PhotoIndexGif.rawValue:
photo.imageData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("giphy", ofType: "gif")!)
caption = "animated GIF";

default:
break
}

photo.attributedCaptionTitle = NSAttributedString(string: "\(photoIndex + 1)", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
photo.attributedCaptionSummary = NSAttributedString(string: caption, attributes: [NSForegroundColorAttributeName: UIColor.lightGrayColor(),NSFontAttributeName:UIFont.preferredFontForTextStyle(UIFontTextStyleBody)])
photo.attributedCaptionCredit = NSAttributedString(string: "NYT Building Photo Credit: Nic Lehoux", attributes: [NSForegroundColorAttributeName: UIColor.grayColor(),NSFontAttributeName:UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1)])

mutablePhotos.append(photo)
}

return mutablePhotos
}()
}()
}


29 changes: 24 additions & 5 deletions Example-Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ViewController: UIViewController, NYTPhotosViewControllerDelegate {
dispatch_after(delayTime, dispatch_get_main_queue()) {
for photo in self.photos {
if photo.image == nil {
photo.image = UIImage(named: PrimaryImageName)
photo.image = UIImage(named: Constants.PrimaryImageName)
photosViewController.updateImageForPhoto(photo)
}
}
Expand All @@ -39,7 +39,7 @@ class ViewController: UIViewController, NYTPhotosViewControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: PrimaryImageName)
let buttonImage = UIImage(named: Constants.PrimaryImageName)
imageButton?.setBackgroundImage(buttonImage, forState: .Normal)
}

Expand Down Expand Up @@ -69,14 +69,21 @@ class ViewController: UIViewController, NYTPhotosViewControllerDelegate {
}

func photosViewController(photosViewController: NYTPhotosViewController, referenceViewForPhoto photo: NYTPhoto) -> UIView? {
if photo as? ExamplePhoto == photos[NoReferenceViewPhotoIndex] {
if photo as? ExamplePhoto == photos[ViewControllerPhotoIndex.NoReferenceView.rawValue] {
return nil
}
return imageButton
}

func photosViewController(photosViewController: NYTPhotosViewController, maximumZoomScaleForPhoto photo: NYTPhoto) -> CGFloat {
if photo as! ExamplePhoto == photos[ViewControllerPhotoIndex.CustomMaxZoomScale.rawValue] {
return 10
}
return 1.0
}

func photosViewController(photosViewController: NYTPhotosViewController, loadingViewForPhoto photo: NYTPhoto) -> UIView? {
if photo as! ExamplePhoto == photos[CustomEverythingPhotoIndex] {
if photo as! ExamplePhoto == photos[ViewControllerPhotoIndex.CustomEverything.rawValue] {
let label = UILabel()
label.text = "Custom Loading..."
label.textColor = UIColor.greenColor()
Expand All @@ -86,7 +93,7 @@ class ViewController: UIViewController, NYTPhotosViewControllerDelegate {
}

func photosViewController(photosViewController: NYTPhotosViewController, captionViewForPhoto photo: NYTPhoto) -> UIView? {
if photo as! ExamplePhoto == photos[CustomEverythingPhotoIndex] {
if photo as! ExamplePhoto == photos[ViewControllerPhotoIndex.CustomEverything.rawValue] {
let label = UILabel()
label.text = "Custom Caption View"
label.textColor = UIColor.whiteColor()
Expand All @@ -96,6 +103,14 @@ class ViewController: UIViewController, NYTPhotosViewControllerDelegate {
return nil
}

func photosViewController(photosViewController: NYTPhotosViewController, titleForPhoto photo: NYTPhoto, atIndex photoIndex: UInt, totalPhotoCount: UInt) -> String? {
if photo as! ExamplePhoto == photos[ViewControllerPhotoIndex.CustomEverything.rawValue] {
return "\(photoIndex+1)/\(totalPhotoCount)"
}
return nil
}


func photosViewController(photosViewController: NYTPhotosViewController, didNavigateToPhoto photo: NYTPhoto, atIndex photoIndex: UInt) {
print("Did Navigate To Photo: \(photo) identifier: \(photoIndex)")
}
Expand All @@ -107,4 +122,8 @@ class ViewController: UIViewController, NYTPhotosViewControllerDelegate {
func photosViewControllerDidDismiss(photosViewController: NYTPhotosViewController) {
print("Did dismiss Photo Viewer: \(photosViewController)")
}




}