Skip to content

Commit

Permalink
optimization for Swift 2.2
Browse files Browse the repository at this point in the history
optimization for Swift 2.2
  • Loading branch information
dingxiao01 committed Jul 4, 2016
1 parent 9aaa8fe commit 42cf057
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DXAlbumTableViewController: UITableViewController {
title = DXlocalizedString("albumTitle", comment: "photos")
createBarButtonItemAtPosition(.Right,
text: DXlocalizedString("cancel", comment: "取消"),
action: Selector("cancelAction")
action: #selector(DXAlbumTableViewController.cancelAction)
)
assetsCollection = DXPickerHelper.fetchAlbumList()
tableView.registerClass(DXAlbumCell.self, forCellReuseIdentifier: dxalbumTableViewCellReuseIdentifier)
Expand All @@ -35,16 +35,14 @@ class DXAlbumTableViewController: UITableViewController {
}

func showUnAuthorizedTipsView() {
tableView.backgroundView = DXUnAuthorizedTipsView(frame: self.tableView.bounds)
tableView.backgroundView = DXUnAuthorizedTipsView(frame: tableView.bounds)
}

// MARK: UIActions

@objc private func cancelAction() {
let photoPicker = navigationController as! DXPhotoPickerController
if (photoPicker.photoPickerDelegate != nil && photoPicker.photoPickerDelegate!.respondsToSelector(Selector("photoPickerDidCancel:"))) {
photoPicker.photoPickerDelegate!.photoPickerDidCancel!(photoPicker)
}
photoPicker.photoPickerDelegate?.photoPickerDidCancel?(photoPicker)
}

// MARK: - Table view data source
Expand Down
51 changes: 29 additions & 22 deletions DXPhotoPicker/Classes/Controllers/DXImageFlowViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DXImageFlowViewController: UIViewController, UIScrollViewDelegate, UIColle
}()
private lazy var sendButton: DXSendButton = {
let button = DXSendButton(frame: CGRectZero)
button.addTarget(self, action: Selector("sendImage"))
button.addTarget(self, action: #selector(DXImageFlowViewController.sendImage))
return button
}()

Expand Down Expand Up @@ -81,19 +81,19 @@ class DXImageFlowViewController: UIViewController, UIScrollViewDelegate, UIColle
.Left,
normalImage: UIImage(named: "back_normal"),
highlightImage: UIImage(named: "back_highlight"),
action: Selector("backButtonAction")
action: #selector(DXImageFlowViewController.backButtonAction)
)
createBarButtonItemAtPosition(
.Right,
text: DXlocalizedString("cancel", comment: "取消"),
action: Selector("cancelAction")
action: #selector(DXImageFlowViewController.cancelAction)
)

let item1 = UIBarButtonItem(
title: DXlocalizedString("preview", comment: "预览"),
style: .Plain,
target: self,
action: Selector("previewAction")
action: #selector(DXImageFlowViewController.previewAction)
)
item1.tintColor = UIColor.blackColor()
item1.enabled = false
Expand Down Expand Up @@ -158,14 +158,12 @@ class DXImageFlowViewController: UIViewController, UIScrollViewDelegate, UIColle
@objc private func sendImage() {

let photoPicker = navigationController as? DXPhotoPickerController
guard (photoPicker != nil && photoPicker!.photoPickerDelegate != nil) else {
guard (photoPicker != nil ) else {
return
}
if (photoPicker!.photoPickerDelegate!.respondsToSelector(Selector("photoPickerController:sendImages:isFullImage:"))) {
DXPickerHelper.saveIdentifier(currentAlbum!.identifier)
DXLog(currentAlbum!.identifier)
photoPicker!.photoPickerDelegate!.photoPickerController!(photoPicker, sendImages: selectedAssetsArray, isFullImage: isFullImage)
}
DXPickerHelper.saveIdentifier(currentAlbum?.identifier)
DXLog(currentAlbum?.identifier)
photoPicker!.photoPickerDelegate?.photoPickerController?(photoPicker, sendImages: selectedAssetsArray, isFullImage: isFullImage)
}

@objc private func backButtonAction() {
Expand All @@ -174,9 +172,7 @@ class DXImageFlowViewController: UIViewController, UIScrollViewDelegate, UIColle

@objc private func cancelAction() {
let navController = navigationController as? DXPhotoPickerController
if (navController != nil && navController!.photoPickerDelegate!.respondsToSelector("photoPickerDidCancel:")) {
navController!.photoPickerDelegate!.photoPickerDidCancel!(navController!)
}
navController?.photoPickerDelegate?.photoPickerDidCancel?(navController!)
}

@objc private func previewAction() {
Expand Down Expand Up @@ -257,34 +253,45 @@ class DXImageFlowViewController: UIViewController, UIScrollViewDelegate, UIColle

// MARK: DXPhotoBroswerDelegate

func sendImagesFromPhotoBrowser(photoBrowser: DXPhotoBrowser, currentAsset: PHAsset) {
func sendImagesFromPhotoBrowser(photoBrowser: DXPhotoBrowser, currentAsset: PHAsset?) {
sendImage()
}

func seletedPhotosNumberInPhotoBrowser(photoBrowser: DXPhotoBrowser) -> Int {
return selectedAssetsArray.count
}

func photoBrowser(photoBrowser: DXPhotoBrowser, currentPhotoAssetIsSeleted asset: PHAsset) -> Bool {
return selectedAssetsArray.contains(asset)
func photoBrowser(photoBrowser: DXPhotoBrowser, currentPhotoAssetIsSeleted asset: PHAsset?) -> Bool {
guard asset != nil else {
return false
}

return selectedAssetsArray.contains(asset!)
}

func photoBrowser(photoBrowser: DXPhotoBrowser, seletedAsset asset: PHAsset) -> Bool {
let index = assetsArray.indexOf(asset)
func photoBrowser(photoBrowser: DXPhotoBrowser, seletedAsset asset: PHAsset?) -> Bool {
guard asset != nil else {
return false
}
let index = assetsArray.indexOf(asset!)
guard index != nil else {
return false
}
let success = addAsset(asset)
let success = addAsset(asset!)
imageFlowCollectionView.reloadItemsAtIndexPaths([NSIndexPath(forItem: index!, inSection: 0)])
return success
}

func photoBrowser(photoBrowser: DXPhotoBrowser, deseletedAsset asset: PHAsset) {
let index = assetsArray.indexOf(asset)
func photoBrowser(photoBrowser: DXPhotoBrowser, deseletedAsset asset: PHAsset?) {
guard asset != nil else {
return
}

let index = assetsArray.indexOf(asset!)
guard index != nil else {
return
}
deleteAsset(asset)
deleteAsset(asset!)
imageFlowCollectionView.reloadItemsAtIndexPaths([NSIndexPath(forItem: index!, inSection: 0)])
}

Expand Down
91 changes: 40 additions & 51 deletions DXPhotoPicker/Classes/Controllers/DXPhotoBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Photos

@objc protocol DXPhotoBroswerDelegate: NSObjectProtocol {

func sendImagesFromPhotoBrowser(photoBrowser: DXPhotoBrowser, currentAsset: PHAsset)
func sendImagesFromPhotoBrowser(photoBrowser: DXPhotoBrowser, currentAsset: PHAsset?)
func seletedPhotosNumberInPhotoBrowser(photoBrowser: DXPhotoBrowser) -> Int
func photoBrowser(photoBrowser: DXPhotoBrowser, currentPhotoAssetIsSeleted asset: PHAsset) -> Bool
func photoBrowser(photoBrowser: DXPhotoBrowser, seletedAsset asset: PHAsset) -> Bool
func photoBrowser(photoBrowser: DXPhotoBrowser, deseletedAsset asset: PHAsset)
func photoBrowser(photoBrowser: DXPhotoBrowser, currentPhotoAssetIsSeleted asset: PHAsset?) -> Bool
func photoBrowser(photoBrowser: DXPhotoBrowser, seletedAsset asset: PHAsset?) -> Bool
func photoBrowser(photoBrowser: DXPhotoBrowser, deseletedAsset asset: PHAsset?)
func photoBrowser(photoBrowser: DXPhotoBrowser, seleteFullImage fullImage: Bool)
}

Expand Down Expand Up @@ -47,7 +47,7 @@ class DXPhotoBrowser: UIViewController, UICollectionViewDelegate, UICollectionVi

lazy var fullImageButton: DXFullImageButton = {
let button = DXFullImageButton(frame: CGRectMake(0, 0, self.view.dx_width/2,28))
button.addTarget(self, action: Selector("fullImageButtonAction"))
button.addTarget(self, action: #selector(DXPhotoBrowser.fullImageButtonAction))
button.selected = self.fullImage
return button
}()
Expand Down Expand Up @@ -81,13 +81,13 @@ class DXPhotoBrowser: UIViewController, UICollectionViewDelegate, UICollectionVi
button.frame = CGRectMake(0, 0, 25, 25)
button.setBackgroundImage(UIImage(named: "photo_check_selected"), forState: .Selected)
button.setBackgroundImage(UIImage(named: "photo_check_default"), forState: .Normal)
button.addTarget(self, action: Selector("checkButtonAction"), forControlEvents: .TouchUpInside)
button.addTarget(self, action: #selector(DXPhotoBrowser.checkButtonAction), forControlEvents: .TouchUpInside)
return button
}()

lazy var sendButton: DXSendButton = {
let button = DXSendButton(frame: CGRectZero)
button.addTarget(self, action: Selector("sendButtonAction"))
button.addTarget(self, action: #selector(DXPhotoBrowser.sendButtonAction))
return button
}()

Expand Down Expand Up @@ -174,10 +174,6 @@ class DXPhotoBrowser: UIViewController, UICollectionViewDelegate, UICollectionVi
navigationController?.setNavigationBarHidden(false, animated: animated)
let navBar = navigationController!.navigationBar
navBar.tintColor = UIColor.whiteColor()
if navBar.respondsToSelector(Selector("setBarTintColor:")) {
navBar.barTintColor = nil
navBar.shadowImage = nil
}
navBar.translucent = true
navBar.barStyle = .BlackTranslucent
navBar.setBackgroundImage(nil, forBarMetrics: .Default)
Expand Down Expand Up @@ -213,19 +209,21 @@ class DXPhotoBrowser: UIViewController, UICollectionViewDelegate, UICollectionVi
setupBarButtonItems()
let rigthBarItem = UIBarButtonItem(customView: checkButton)
navigationItem.rightBarButtonItem = rigthBarItem
createBarButtonItemAtPosition(.Left, normalImage: UIImage(named: "back_normal"), highlightImage: UIImage(named: "back_highlight"), action: Selector("backButtonAction"))
createBarButtonItemAtPosition(.Left, normalImage: UIImage(named: "back_normal"), highlightImage: UIImage(named: "back_highlight"), action: #selector(DXPhotoBrowser.backButtonAction))
}

private func updateNavigationBarAndToolBar() {
guard photosDataSource != nil else {
return
}
title = "\(currentIndex + 1)"+"/" + "\(photosDataSource!.count)"
var selectTag = false
if (self.delegate != nil && self.delegate!.respondsToSelector(Selector("photoBrowser:currentPhotoAssetIsSeleted:"))) {
selectTag = self.delegate!.photoBrowser(self, currentPhotoAssetIsSeleted: photosDataSource![currentIndex])
let selected = self.delegate?.photoBrowser(self, currentPhotoAssetIsSeleted: photosDataSource?[currentIndex])
if selected == nil {
checkButton.selected = false
} else {
checkButton.selected = selected!
}
checkButton.selected = selectTag

fullImageButton.selected = fullImage
if fullImage {
let asset = photosDataSource![currentIndex]
Expand All @@ -236,10 +234,11 @@ class DXPhotoBrowser: UIViewController, UICollectionViewDelegate, UICollectionVi
}

private func updateSelestedNumber() {
if (self.delegate != nil && self.delegate!.respondsToSelector("seletedPhotosNumberInPhotoBrowser:")) {
let number = self.delegate!.seletedPhotosNumberInPhotoBrowser(self)
self.sendButton.badgeValue = "\(number)"
let number = self.delegate?.seletedPhotosNumberInPhotoBrowser(self)
if (number != nil) {
self.sendButton.badgeValue = "\(number!)"
}

}

private func didScrollToPage(page: Int) {
Expand All @@ -254,46 +253,35 @@ class DXPhotoBrowser: UIViewController, UICollectionViewDelegate, UICollectionVi
}

@objc private func sendButtonAction() {
if (self.delegate != nil && self.delegate!.respondsToSelector(Selector("sendImagesFromPhotoBrowser:currentAsset:"))) {
self.delegate!.sendImagesFromPhotoBrowser(self, currentAsset: photosDataSource![currentIndex])
}
delegate?.sendImagesFromPhotoBrowser(self, currentAsset: photosDataSource![currentIndex])
}

@objc private func fullImageButtonAction() {
fullImageButton.selected = !fullImageButton.selected
fullImage = fullImageButton.selected
guard delegate != nil else {
return
}
if self.delegate!.respondsToSelector(Selector("photoBrowser:seleteFullImage:")) {
self.delegate!.photoBrowser(self, seleteFullImage: fullImage)
}

self.delegate?.photoBrowser(self, seleteFullImage: fullImage)
if fullImageButton.selected {
if delegate!.photoBrowser(self, seletedAsset: photosDataSource![currentIndex]) {
updateSelestedNumber()
if ((delegate?.photoBrowser(self, seletedAsset: photosDataSource?[currentIndex])) != nil){
updateNavigationBarAndToolBar()
}
updateNavigationBarAndToolBar()
}
}

@objc private func checkButtonAction() {
if checkButton.selected {
guard self.delegate != nil else {
DXLog("have not set the browser's delegate")
return
}
if (self.delegate!.respondsToSelector(Selector("photoBrowser:deseletedAsset:"))) {
self.delegate!.photoBrowser(self, deseletedAsset: photosDataSource![currentIndex])
delegate?.photoBrowser(self, deseletedAsset: photosDataSource?[currentIndex])
checkButton.selected = false
updateSelestedNumber()
} else {
let selected = delegate?.photoBrowser(self, seletedAsset: photosDataSource?[currentIndex])
if selected == nil {
checkButton.selected = false
updateSelestedNumber()
} else {
checkButton.selected = selected!
}
} else {
if self.delegate!.respondsToSelector(Selector("photoBrowser:seletedAsset:")) {
checkButton.selected = self.delegate!.photoBrowser(self, seletedAsset: photosDataSource![currentIndex])
if checkButton.selected {
updateSelestedNumber()
}

if checkButton.selected {
updateSelestedNumber()
}
}
}
Expand Down Expand Up @@ -337,25 +325,26 @@ class DXPhotoBrowser: UIViewController, UICollectionViewDelegate, UICollectionVi

// MARK: control hide

private func setControlsHidden(var hidden: Bool, animated: Bool) {
private func setControlsHidden(hidden: Bool, animated: Bool) {
var hide = hidden
if (photosDataSource == nil || photosDataSource!.count == 0) {
hidden = false
hide = false
}
let animationOffSet: CGFloat = 20
let animationDuration = (animated ? 0.35 : 0)
statusBarShouldBeHidden = hidden
statusBarShouldBeHidden = hide
UIView.animateWithDuration(animationDuration, animations: {[unowned self] () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
})
let frame = CGRectIntegral(CGRectMake(0, view.dx_height - 44, view.dx_width, 44))
if areControlsHidden() && hidden == false && animated {
if areControlsHidden() && hide == false && animated {
toolBar.frame = CGRectOffset(frame, 0, animationOffSet)
}
UIView.animateWithDuration(animationDuration) {[unowned self] () -> Void in
let alpha: CGFloat = hidden ? 0 : 1
let alpha: CGFloat = hide ? 0 : 1
self.navigationController?.navigationBar.alpha = alpha
self.toolBar.frame = frame
if hidden {
if hide {
self.toolBar.frame = CGRectOffset(self.toolBar.frame, 0, animationOffSet)
}
self.toolBar.alpha = alpha
Expand Down
2 changes: 1 addition & 1 deletion DXPhotoPicker/Classes/Views/DXAssetCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DXAssetCell: UICollectionViewCell {
private lazy var checkButton: UIButton = {
let button = UIButton(type: .Custom)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: Selector("checkButtonAction"), forControlEvents: .TouchUpInside)
button.addTarget(self, action: #selector(DXAssetCell.checkButtonAction), forControlEvents: .TouchUpInside)
return button
}()

Expand Down
2 changes: 1 addition & 1 deletion DXPhotoPicker/Classes/Views/DXBrowserCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class DXBrowserCell: UICollectionViewCell, UIScrollViewDelegate, DXTapDetectingI
guard photoBrowser != nil else {
return
}
photoBrowser?.performSelector(Selector("toggleControls"), withObject: nil, afterDelay: 0.2)
photoBrowser?.performSelector(#selector(DXPhotoBrowser.toggleControls), withObject: nil, afterDelay: 0.2)
}

guard touch != nil else {
Expand Down
12 changes: 3 additions & 9 deletions DXPhotoPicker/Classes/Views/DXTapDetectingImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,15 @@ class DXTapDetectingImageView: UIImageView, DXTapDetectingImageViewDelegate {
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

func handleSingleTap(touch: UITouch?) {
if (self.tapDelegate != nil && self.tapDelegate!.respondsToSelector(Selector("imageView:singleTapDetected:"))) {
self.tapDelegate?.imageView!(self, singleTapDetected: touch)
}
tapDelegate?.imageView?(self, singleTapDetected: touch)
}

func handleDoubleTap(touch: UITouch?) {
if (self.tapDelegate != nil && self.tapDelegate!.respondsToSelector(Selector("imageView:doubleTapDetected:"))) {
self.tapDelegate?.imageView!(self, doubleTapDetected: touch)
}
tapDelegate?.imageView?(self, doubleTapDetected: touch)
}

func handleTripleTap(touch: UITouch?) {
if (self.tapDelegate != nil && self.tapDelegate!.respondsToSelector(Selector("imageView:tripleTapDetected:"))) {
self.tapDelegate?.imageView!(self, tripleTapDetected: touch)
}
tapDelegate?.imageView?(self, tripleTapDetected: touch)
}

let touch = touches.first
Expand Down
2 changes: 1 addition & 1 deletion DXPhotoPicker/Support/DXUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public func DXLog<T>(message: T, file: String = __FILE__, method: String = __FUNCTION__,line: Int = __LINE__) {
public func DXLog<T>(message: T, file: String = #file, method: String = #function,line: Int = #line) {
#if DEBUG
print("\((file as NSString).lastPathComponent)[line:\(line)], \(method): \(message)")
#endif
Expand Down

0 comments on commit 42cf057

Please sign in to comment.