Last active
January 12, 2016 13:29
-
-
Save hansemannn/78233d00aeb774cf9739 to your computer and use it in GitHub Desktop.
Titanium Mobile proxies for PHLivePhoto's using Swift 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Appcelerator Titanium Mobile | |
* Copyright (c) 2009-2015 by Appcelerator, Inc. All Rights Reserved. | |
* Licensed under the terms of the Apache Public License | |
* Please see the LICENSE included with this distribution for details. | |
*/ | |
import Foundation | |
import Photos | |
@available(iOS 9.1, *) | |
@objc class TiUIiOSLivePhoto : TiProxy { | |
var livePhoto: PHLivePhoto! | |
init(livePhoto: PHLivePhoto) { | |
self.livePhoto = livePhoto | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Appcelerator Titanium Mobile | |
* Copyright (c) 2009-2015 by Appcelerator, Inc. All Rights Reserved. | |
* Licensed under the terms of the Apache Public License | |
* Please see the LICENSE included with this distribution for details. | |
*/ | |
import Foundation | |
import PhotosUI | |
@available(iOS 9.1, *) | |
@objc class TiUIiOSLivePhotoView : TiUIView, PHLivePhotoViewDelegate { | |
var width: TiDimension! | |
var height: TiDimension! | |
var autoWidth: CGFloat! | |
var autoHeight: CGFloat! | |
var livePhotoView: PHLivePhotoView! | |
func getLivePhotoViewProxy() -> TiUIiOSLivePhotoViewProxy { | |
return self.proxy as! TiUIiOSLivePhotoViewProxy | |
} | |
func getLivePhotoView() -> PHLivePhotoView { | |
if self.livePhotoView == nil { | |
self.livePhotoView = PHLivePhotoView(frame: self.bounds) | |
self.livePhotoView.delegate = self | |
self.livePhotoView.autoresizingMask = .FlexibleHeight | |
self.livePhotoView.contentMode = self.contentModeForLivePhotoView() | |
self.addSubview(self.livePhotoView) | |
} | |
return self.livePhotoView | |
} | |
// MARK: - Public API's | |
func setLivePhoto_(arg: TiUIiOSLivePhoto) { | |
let livePhoto: PHLivePhoto = arg.livePhoto! | |
autoWidth = livePhoto.size.width | |
autoHeight = livePhoto.size.height | |
self.livePhotoView.livePhoto = livePhoto | |
} | |
func setWidth_(width_: AnyObject) { | |
width = TiDimensionFromObject(width_) | |
self.updateContentMode() | |
} | |
func setHeight_(height_: AnyObject) { | |
height = TiDimensionFromObject(height_) | |
self.updateContentMode() | |
} | |
override func frameSizeChanged(frame: CGRect, bounds: CGRect) { | |
for child: UIView in self.subviews { | |
TiUtils.setView(child, positionRect: bounds) | |
} | |
super.frameSizeChanged(frame, bounds: bounds) | |
} | |
// MARK: - Helper | |
func contentModeForLivePhotoView() -> UIViewContentMode { | |
if TiDimensionIsAuto(width) || TiDimensionIsAutoSize(width) || TiDimensionIsUndefined(width) || | |
TiDimensionIsAuto(height) || TiDimensionIsAutoSize(height) || TiDimensionIsUndefined(height) { | |
return .ScaleAspectFit; | |
} else { | |
return .ScaleToFill; | |
} | |
} | |
func updateContentMode() { | |
if self.livePhotoView != nil { | |
getLivePhotoView().contentMode = self.contentModeForLivePhotoView() | |
} | |
} | |
override func contentWidthForWidth(suggestedWidth: CGFloat) -> CGFloat { | |
if autoWidth > 0 { | |
// If height is DIP returned a scaled autowidth to maintain aspect ratio | |
if TiDimensionIsDip(height) && autoHeight > 0 { | |
return round(autoWidth * height.value / autoHeight); | |
} | |
return autoWidth; | |
} | |
let calculatedWidth: CGFloat = TiDimensionCalculateValue(width, autoWidth) | |
if calculatedWidth > 0 { | |
return calculatedWidth; | |
} | |
return 0; | |
} | |
override func contentHeightForWidth(width_: CGFloat) -> CGFloat { | |
if width_ != autoWidth && autoWidth > 0 && autoHeight > 0 { | |
return (width_ * autoHeight / autoWidth); | |
} | |
if autoHeight > 0 { | |
return autoHeight; | |
} | |
let calculatedHeight: CGFloat = TiDimensionCalculateValue(height, autoHeight) | |
if calculatedHeight > 0 { | |
return calculatedHeight; | |
} | |
return 0; | |
} | |
// MARK: - Delegates | |
func livePhotoView(livePhotoView: PHLivePhotoView, willBeginPlaybackWithStyle playbackStyle: PHLivePhotoViewPlaybackStyle) { | |
let event: Dictionary = ["playbackStyle" : NSNumber(integer: playbackStyle.rawValue)] | |
self.getLivePhotoViewProxy().fireEvent("start", withObject: event) | |
} | |
func livePhotoView(livePhotoView: PHLivePhotoView, didEndPlaybackWithStyle playbackStyle: PHLivePhotoViewPlaybackStyle) { | |
let event: Dictionary = ["playbackStyle" : NSNumber(integer: playbackStyle.rawValue)] | |
self.getLivePhotoViewProxy().fireEvent("stop", withObject: event) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Appcelerator Titanium Mobile | |
* Copyright (c) 2009-2015 by Appcelerator, Inc. All Rights Reserved. | |
* Licensed under the terms of the Apache Public License | |
* Please see the LICENSE included with this distribution for details. | |
*/ | |
import Foundation | |
import PhotosUI | |
@available(iOS 9.1, *) | |
@objc class TiUIiOSLivePhotoViewProxy : TiViewProxy { | |
// MARK: Proxy lifecyle | |
override func apiName() -> String! { | |
return "Ti.UI.iOS.LivePhotoView" | |
} | |
// MARK: Public API's | |
func livePhotoView() -> TiUIiOSLivePhotoView { | |
return self.view as! TiUIiOSLivePhotoView | |
} | |
func startPlaybackWithStyle(args: AnyObject) { | |
self.livePhotoView().livePhotoView.startPlaybackWithStyle(PHLivePhotoViewPlaybackStyle(rawValue: args.objectAtIndex(0) as! Int)!) | |
} | |
func stopPlayback(unused: AnyObject) { | |
self.livePhotoView().livePhotoView.stopPlayback() | |
} | |
func setMuted(value: AnyObject) { | |
self.replaceValue(value, forKey: "muted", notification: false) | |
self.livePhotoView().livePhotoView.muted = TiUtils.boolValue(value) | |
} | |
func muted() -> NSNumber { | |
return self.livePhotoView().livePhotoView.muted | |
} | |
// MARK: Helper | |
override func defaultAutoWidthBehavior(unused: AnyObject!) -> TiDimension { | |
return TiDimensionAutoSize | |
} | |
override func defaultAutoHeightBehavior(unused: AnyObject!) -> TiDimension { | |
return TiDimensionAutoSize | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment