Skip to content

Commit

Permalink
Merge branch '#111'
Browse files Browse the repository at this point in the history
  • Loading branch information
1and2papa committed Oct 12, 2015
2 parents 56f8edc + e4cc2c9 commit 528a95a
Show file tree
Hide file tree
Showing 36 changed files with 447 additions and 25 deletions.
1 change: 1 addition & 0 deletions CTAssetsPickerController/CTAssetItemViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

@interface CTAssetItemViewController : UIViewController

@property (nonatomic, assign) BOOL allowsSelection;
@property (nonatomic, strong, readonly) PHAsset *asset;
@property (nonatomic, strong, readonly) UIImage *image;

Expand Down
133 changes: 131 additions & 2 deletions CTAssetsPickerController/CTAssetItemViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal


#import <PureLayout/PureLayout.h>
#import "CTAssetsPickerController.h"
#import "CTAssetItemViewController.h"
#import "CTAssetScrollView.h"
#import "NSBundle+CTAssetsPickerController.h"
Expand All @@ -36,6 +37,8 @@ of this software and associated documentation files (the "Software"), to deal

@interface CTAssetItemViewController ()

@property (nonatomic, weak) CTAssetsPickerController *picker;

@property (nonatomic, strong) PHAsset *asset;
@property (nonatomic, strong) UIImage *image;

Expand Down Expand Up @@ -65,6 +68,7 @@ - (instancetype)initWithAsset:(PHAsset *)asset
{
_imageManager = [PHImageManager defaultManager];
self.asset = asset;
self.allowsSelection = NO;
}

return self;
Expand All @@ -80,6 +84,7 @@ - (void)viewDidLoad
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setupScrollViewButtons];
[self requestAssetImage];
}

Expand Down Expand Up @@ -108,18 +113,41 @@ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIVi
}


#pragma mark - Accessors

- (CTAssetsPickerController *)picker
{
return (CTAssetsPickerController *)self.splitViewController.parentViewController;
}


#pragma mark - Setup

- (void)setupViews
{
CTAssetScrollView *scrollView = [CTAssetScrollView newAutoLayoutView];
[scrollView.playButton addTarget:self action:@selector(playAsset:) forControlEvents:UIControlEventTouchUpInside];
scrollView.allowsSelection = self.allowsSelection;

self.scrollView = scrollView;
[self.view addSubview:self.scrollView];

[self.view layoutIfNeeded];
}

- (void)setupScrollViewButtons
{
CTAssetPlayButton *playButton = self.scrollView.playButton;
[playButton addTarget:self action:@selector(playAsset:) forControlEvents:UIControlEventTouchUpInside];

CTAssetSelectionButton *selectionButton = self.scrollView.selectionButton;

selectionButton.enabled = [self assetScrollView:self.scrollView shouldEnableAsset:self.asset];
selectionButton.selected = [self.picker.selectedAssets containsObject:self.asset];

[selectionButton addTarget:self action:@selector(selectionButtonTouchDown:) forControlEvents:UIControlEventTouchDown];
[selectionButton addTarget:self action:@selector(selectionButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - Cancel request

- (void)cancelRequestAsset
Expand Down Expand Up @@ -290,4 +318,105 @@ - (void)pauseAsset:(id)sender
}


#pragma mark - Selection

- (void)selectionButtonTouchDown:(id)sender
{
PHAsset *asset = self.asset;
CTAssetScrollView *scrollView = self.scrollView;

if ([self assetScrollView:scrollView shouldHighlightAsset:asset])
[self assetScrollView:scrollView didHighlightAsset:asset];
}

- (void)selectionButtonTouchUpInside:(id)sender
{
PHAsset *asset = self.asset;
CTAssetScrollView *scrollView = self.scrollView;
CTAssetSelectionButton *selectionButton = scrollView.selectionButton;


if (!selectionButton.selected)
{
if ([self assetScrollView:scrollView shouldSelectAsset:asset])
{
[self.picker selectAsset:asset];
[selectionButton setSelected:YES];
[self assetScrollView:scrollView didSelectAsset:asset];
}
}

else
{
if ([self assetScrollView:scrollView shouldDeselectAsset:asset])
{
[self.picker deselectAsset:asset];
[selectionButton setSelected:NO];
[self assetScrollView:scrollView didDeselectAsset:asset];
}
}

[self assetScrollView:self.scrollView didUnhighlightAsset:self.asset];
}


#pragma mark - Asset scrollView delegate

- (BOOL)assetScrollView:(CTAssetScrollView *)scrollView shouldEnableAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:shouldEnableAsset:)])
return [self.picker.delegate assetsPickerController:self.picker shouldEnableAsset:asset];
else
return YES;
}

- (BOOL)assetScrollView:(CTAssetScrollView *)scrollView shouldSelectAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:shouldSelectAsset:)])
return [self.picker.delegate assetsPickerController:self.picker shouldSelectAsset:asset];
else
return YES;
}

- (void)assetScrollView:(CTAssetScrollView *)scrollView didSelectAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:didSelectAsset:)])
[self.picker.delegate assetsPickerController:self.picker didSelectAsset:asset];
}

- (BOOL)assetScrollView:(CTAssetScrollView *)scrollView shouldDeselectAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:shouldDeselectAsset:)])
return [self.picker.delegate assetsPickerController:self.picker shouldDeselectAsset:asset];
else
return YES;
}

- (void)assetScrollView:(CTAssetScrollView *)scrollView didDeselectAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:didDeselectAsset:)])
[self.picker.delegate assetsPickerController:self.picker didDeselectAsset:asset];
}

- (BOOL)assetScrollView:(CTAssetScrollView *)scrollView shouldHighlightAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:shouldHighlightAsset:)])
return [self.picker.delegate assetsPickerController:self.picker shouldHighlightAsset:asset];
else
return YES;
}

- (void)assetScrollView:(CTAssetScrollView *)scrollView didHighlightAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:didHighlightAsset:)])
[self.picker.delegate assetsPickerController:self.picker didHighlightAsset:asset];
}

- (void)assetScrollView:(CTAssetScrollView *)scrollView didUnhighlightAsset:(PHAsset *)asset
{
if ([self.picker.delegate respondsToSelector:@selector(assetsPickerController:didUnhighlightAsset:)])
[self.picker.delegate assetsPickerController:self.picker didUnhighlightAsset:asset];
}


@end
4 changes: 4 additions & 0 deletions CTAssetsPickerController/CTAssetPlayButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ - (void)setupColorView
colorView.backgroundColor = [UIColor whiteColor];
colorView.userInteractionEnabled = NO;
self.colorView = colorView;

[self addSubview:self.colorView];
}

Expand Down Expand Up @@ -167,6 +168,9 @@ - (void)updateConstraints
[super updateConstraints];
}


#pragma mark - States

- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
Expand Down
4 changes: 4 additions & 0 deletions CTAssetsPickerController/CTAssetScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import <UIKit/UIKit.h>
#import "CTAssetItemViewController.h"
#import "CTAssetPlayButton.h"
#import "CTAssetSelectionButton.h"



Expand All @@ -38,11 +39,14 @@ extern NSString * const CTAssetScrollViewPlayerWillPauseNotification;

@interface CTAssetScrollView : UIScrollView

@property (nonatomic, assign) BOOL allowsSelection;

@property (nonatomic, strong, readonly) UIImage *image;
@property (nonatomic, strong, readonly) AVPlayer *player;

@property (nonatomic, strong, readonly) UIImageView *imageView;
@property (nonatomic, strong, readonly) CTAssetPlayButton *playButton;
@property (nonatomic, strong, readonly) CTAssetSelectionButton *selectionButton;


- (void)startActivityAnimating;
Expand Down
40 changes: 33 additions & 7 deletions CTAssetsPickerController/CTAssetScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ @interface CTAssetScrollView ()
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
@property (nonatomic, strong) CTAssetPlayButton *playButton;
@property (nonatomic, strong) CTAssetSelectionButton *selectionButton;

@property (nonatomic, assign) BOOL shouldUpdateConstraints;
@property (nonatomic, assign) BOOL didSetupConstraints;
Expand All @@ -77,6 +78,7 @@ - (instancetype)initWithFrame:(CGRect)frame
if (self)
{
_shouldUpdateConstraints = YES;
self.allowsSelection = NO;
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = YES;
Expand Down Expand Up @@ -106,25 +108,25 @@ - (void)setupViews
imageView.isAccessibilityElement = YES;
imageView.accessibilityTraits = UIAccessibilityTraitImage;
self.imageView = imageView;

[self addSubview:self.imageView];

UIProgressView *progressView =
[[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.progressView = progressView;

[self addSubview:self.progressView];

UIActivityIndicatorView *activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

self.activityView = activityView;

[self addSubview:self.activityView];

CTAssetPlayButton *playButton = [CTAssetPlayButton newAutoLayoutView];
self.playButton = playButton;
[self addSubview:self.playButton];

CTAssetSelectionButton *selectionButton = [CTAssetSelectionButton newAutoLayoutView];
self.selectionButton = selectionButton;
[self addSubview:self.selectionButton];
}


Expand All @@ -134,10 +136,11 @@ - (void)updateConstraints
{
if (!self.didSetupConstraints)
{
[self updateSelectionButtonIfNeeded];
[self autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
[self updateProgressConstraints];
[self updateActivityConstraints];
[self updateButtonConstraints];
[self updateButtonsConstraints];

self.didSetupConstraints = YES;
}
Expand All @@ -146,6 +149,15 @@ - (void)updateConstraints
[super updateConstraints];
}

- (void)updateSelectionButtonIfNeeded
{
if (!self.allowsSelection)
{
[self.selectionButton removeFromSuperview];
self.selectionButton = nil;
}
}

- (void)updateProgressConstraints
{
[UIView autoSetPriority:UILayoutPriorityDefaultLow forConstraints:^{
Expand All @@ -167,10 +179,20 @@ - (void)updateActivityConstraints
[self.activityView autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.superview];
}

- (void)updateButtonConstraints
- (void)updateButtonsConstraints
{
[self.playButton autoAlignAxis:ALAxisVertical toSameAxisOfView:self.superview];
[self.playButton autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.superview];

[UIView autoSetPriority:UILayoutPriorityDefaultLow forConstraints:^{
[self.selectionButton autoConstrainAttribute:ALAttributeTrailing toAttribute:ALAttributeTrailing ofView:self.superview withOffset:-self.layoutMargins.right relation:NSLayoutRelationEqual];
[self.selectionButton autoConstrainAttribute:ALAttributeBottom toAttribute:ALAttributeBottom ofView:self.superview withOffset:-self.layoutMargins.bottom relation:NSLayoutRelationEqual];
}];

[UIView autoSetPriority:UILayoutPriorityDefaultHigh forConstraints:^{
[self.selectionButton autoConstrainAttribute:ALAttributeTrailing toAttribute:ALAttributeTrailing ofView:self.imageView withOffset:-self.layoutMargins.right relation:NSLayoutRelationLessThanOrEqual];
[self.selectionButton autoConstrainAttribute:ALAttributeBottom toAttribute:ALAttributeBottom ofView:self.imageView withOffset:-self.layoutMargins.bottom relation:NSLayoutRelationLessThanOrEqual];
}];
}

- (void)updateContentFrame
Expand All @@ -194,13 +216,15 @@ - (void)updateContentFrame
- (void)startActivityAnimating
{
[self.playButton setHidden:YES];
[self.selectionButton setHidden:YES];
[self.activityView startAnimating];
[self postPlayerWillPlayNotification];
}

- (void)stopActivityAnimating
{
[self.playButton setHidden:NO];
[self.selectionButton setHidden:NO];
[self.activityView stopAnimating];
[self postPlayerWillPauseNotification];
}
Expand Down Expand Up @@ -513,7 +537,7 @@ - (void)scrollViewDidZoom:(UIScrollView *)scrollView

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return ![touch.view isDescendantOfView:self.playButton];
return !([touch.view isDescendantOfView:self.playButton] || [touch.view isDescendantOfView:self.selectionButton]);
}


Expand Down Expand Up @@ -633,13 +657,15 @@ - (void)playerDidPlay:(id)sender
{
[self setProgress:1];
[self.playButton setHidden:YES];
[self.selectionButton setHidden:YES];
[self.activityView stopAnimating];
}


- (void)playerDidPause:(id)sender
{
[self.playButton setHidden:NO];
[self.selectionButton setHidden:NO];
}

- (void)playerDidLoadItem:(id)sender
Expand Down
Loading

0 comments on commit 528a95a

Please sign in to comment.