Commit c856c007 authored by tanzhen's avatar tanzhen

新增单选模式,设置maxImagesCount为1即可

parent 20be4328
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<dependencies> <dependencies>
<deployment identifier="iOS"/> <deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4Ae-5T-gEc" userLabel="TopContentView"> <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4Ae-5T-gEc" userLabel="TopContentView">
<rect key="frame" x="0.0" y="0.0" width="320" height="300"/> <rect key="frame" x="0.0" y="0.0" width="320" height="344"/>
<subviews> <subviews>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="obE-hs-XLa" userLabel="View1"> <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="obE-hs-XLa" userLabel="View1">
<rect key="frame" x="0.0" y="20" width="320" height="44"/> <rect key="frame" x="0.0" y="20" width="320" height="44"/>
...@@ -125,6 +125,23 @@ ...@@ -125,6 +125,23 @@
</subviews> </subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view> </view>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="PA2-zk-4Ey" userLabel="View7">
<rect key="frame" x="0.0" y="284" width="320" height="44"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="照片最大可选张数" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rK3-1U-kcW">
<rect key="frame" x="0.0" y="0.0" width="200" height="44"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="9" borderStyle="roundedRect" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="37K-cO-hgf">
<rect key="frame" x="220" y="7" width="80" height="30"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
</textField>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
</subviews> </subviews>
</view> </view>
</subviews> </subviews>
...@@ -135,6 +152,7 @@ ...@@ -135,6 +152,7 @@
<outlet property="allowPickingImageSwitch" destination="iRQ-aE-1KG" id="V7a-u6-cLv"/> <outlet property="allowPickingImageSwitch" destination="iRQ-aE-1KG" id="V7a-u6-cLv"/>
<outlet property="allowPickingOriginalPhotoSwitch" destination="4L3-Da-pcd" id="KFm-Zl-dpD"/> <outlet property="allowPickingOriginalPhotoSwitch" destination="4L3-Da-pcd" id="KFm-Zl-dpD"/>
<outlet property="allowPickingVideoSwitch" destination="a58-aK-ZB0" id="9Ti-hW-QRj"/> <outlet property="allowPickingVideoSwitch" destination="a58-aK-ZB0" id="9Ti-hW-QRj"/>
<outlet property="maxCountTF" destination="37K-cO-hgf" id="776-gE-tea"/>
<outlet property="showSheetSwitch" destination="uHt-bT-lBZ" id="NxK-wM-ivE"/> <outlet property="showSheetSwitch" destination="uHt-bT-lBZ" id="NxK-wM-ivE"/>
<outlet property="showTakePhotoBtnSwitch" destination="jpa-B9-8n6" id="lh8-2O-IWS"/> <outlet property="showTakePhotoBtnSwitch" destination="jpa-B9-8n6" id="lh8-2O-IWS"/>
<outlet property="sortAscendingSwitch" destination="Rbt-bh-r0x" id="WEA-Vq-uSg"/> <outlet property="sortAscendingSwitch" destination="Rbt-bh-r0x" id="WEA-Vq-uSg"/>
......
...@@ -29,6 +29,8 @@ typedef enum : NSUInteger { ...@@ -29,6 +29,8 @@ typedef enum : NSUInteger {
@property (nonatomic, copy) NSString *photoSelImageName; @property (nonatomic, copy) NSString *photoSelImageName;
@property (nonatomic, copy) NSString *photoDefImageName; @property (nonatomic, copy) NSString *photoDefImageName;
@property (nonatomic, assign) NSInteger maxImagesCount;
@end @end
......
...@@ -69,6 +69,12 @@ ...@@ -69,6 +69,12 @@
} }
} }
- (void)setMaxImagesCount:(NSInteger)maxImagesCount {
_maxImagesCount = maxImagesCount;
self.selectPhotoButton.hidden = maxImagesCount == 1;
self.selectImageView.hidden = maxImagesCount == 1;
}
- (void)setType:(TZAssetCellType)type { - (void)setType:(TZAssetCellType)type {
_type = type; _type = type;
if (type == TZAssetCellTypePhoto || type == TZAssetCellTypeLivePhoto) { if (type == TZAssetCellTypePhoto || type == TZAssetCellTypeLivePhoto) {
......
...@@ -107,6 +107,8 @@ static CGSize AssetGridThumbnailSize; ...@@ -107,6 +107,8 @@ static CGSize AssetGridThumbnailSize;
} }
- (void)configCollectionView { - (void)configCollectionView {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
CGFloat margin = 4; CGFloat margin = 4;
CGFloat itemWH = (self.view.tz_width - 2 * margin - 4) / 4 - margin; CGFloat itemWH = (self.view.tz_width - 2 * margin - 4) / 4 - margin;
...@@ -115,7 +117,8 @@ static CGSize AssetGridThumbnailSize; ...@@ -115,7 +117,8 @@ static CGSize AssetGridThumbnailSize;
layout.minimumLineSpacing = margin; layout.minimumLineSpacing = margin;
CGFloat top = margin + 44; CGFloat top = margin + 44;
if (iOS7Later) top += 20; if (iOS7Later) top += 20;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(margin, top, self.view.tz_width - 2 * margin, self.view.tz_height - 50 - top) collectionViewLayout:layout]; CGFloat collectionViewHeight = tzImagePickerVc.maxImagesCount > 1 ? self.view.tz_height - 50 - top : self.view.tz_height - top;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(margin, top, self.view.tz_width - 2 * margin, collectionViewHeight) collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self; _collectionView.dataSource = self;
_collectionView.delegate = self; _collectionView.delegate = self;
...@@ -123,7 +126,6 @@ static CGSize AssetGridThumbnailSize; ...@@ -123,7 +126,6 @@ static CGSize AssetGridThumbnailSize;
if (iOS7Later) _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 2); if (iOS7Later) _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 2);
_collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2); _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2);
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_showTakePhotoBtn && tzImagePickerVc.allowTakePicture ) { if (_showTakePhotoBtn && tzImagePickerVc.allowTakePicture ) {
_collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 4) / 4) * self.view.tz_width); _collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 4) / 4) * self.view.tz_width);
} else { } else {
...@@ -155,6 +157,8 @@ static CGSize AssetGridThumbnailSize; ...@@ -155,6 +157,8 @@ static CGSize AssetGridThumbnailSize;
- (void)configBottomToolBar { - (void)configBottomToolBar {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (tzImagePickerVc.maxImagesCount <= 1) return;
UIView *bottomToolBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.tz_height - 50, self.view.tz_width, 50)]; UIView *bottomToolBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.tz_height - 50, self.view.tz_width, 50)];
CGFloat rgb = 253 / 255.0; CGFloat rgb = 253 / 255.0;
bottomToolBar.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0]; bottomToolBar.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
...@@ -163,6 +167,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -163,6 +167,7 @@ static CGSize AssetGridThumbnailSize;
CGFloat previewWidth = [previewText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.width; CGFloat previewWidth = [previewText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.width;
_previewButton = [UIButton buttonWithType:UIButtonTypeCustom]; _previewButton = [UIButton buttonWithType:UIButtonTypeCustom];
_previewButton.frame = CGRectMake(10, 3, previewWidth + 2, 44); _previewButton.frame = CGRectMake(10, 3, previewWidth + 2, 44);
_previewButton.tz_width = tzImagePickerVc.maxImagesCount <= 1 ? 0 : previewWidth + 2;
[_previewButton addTarget:self action:@selector(previewButtonClick) forControlEvents:UIControlEventTouchUpInside]; [_previewButton addTarget:self action:@selector(previewButtonClick) forControlEvents:UIControlEventTouchUpInside];
_previewButton.titleLabel.font = [UIFont systemFontOfSize:16]; _previewButton.titleLabel.font = [UIFont systemFontOfSize:16];
[_previewButton setTitle:previewText forState:UIControlStateNormal]; [_previewButton setTitle:previewText forState:UIControlStateNormal];
...@@ -208,7 +213,6 @@ static CGSize AssetGridThumbnailSize; ...@@ -208,7 +213,6 @@ static CGSize AssetGridThumbnailSize;
_numberImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamedFromMyBundle:tzImagePickerVc.photoNumberIconImageName]]; _numberImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamedFromMyBundle:tzImagePickerVc.photoNumberIconImageName]];
_numberImageView.frame = CGRectMake(self.view.tz_width - 56 - 24, 12, 26, 26); _numberImageView.frame = CGRectMake(self.view.tz_width - 56 - 24, 12, 26, 26);
_numberImageView.hidden = tzImagePickerVc.selectedModels.count <= 0;
_numberImageView.backgroundColor = [UIColor clearColor]; _numberImageView.backgroundColor = [UIColor clearColor];
_numberLable = [[UILabel alloc] init]; _numberLable = [[UILabel alloc] init];
...@@ -217,7 +221,6 @@ static CGSize AssetGridThumbnailSize; ...@@ -217,7 +221,6 @@ static CGSize AssetGridThumbnailSize;
_numberLable.textColor = [UIColor whiteColor]; _numberLable.textColor = [UIColor whiteColor];
_numberLable.textAlignment = NSTextAlignmentCenter; _numberLable.textAlignment = NSTextAlignmentCenter;
_numberLable.text = [NSString stringWithFormat:@"%zd",tzImagePickerVc.selectedModels.count]; _numberLable.text = [NSString stringWithFormat:@"%zd",tzImagePickerVc.selectedModels.count];
_numberLable.hidden = tzImagePickerVc.selectedModels.count <= 0;
_numberLable.backgroundColor = [UIColor clearColor]; _numberLable.backgroundColor = [UIColor clearColor];
UIView *divide = [[UIView alloc] init]; UIView *divide = [[UIView alloc] init];
...@@ -336,6 +339,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -336,6 +339,7 @@ static CGSize AssetGridThumbnailSize;
model = _models[indexPath.row - 1]; model = _models[indexPath.row - 1];
} }
cell.model = model; cell.model = model;
cell.maxImagesCount = tzImagePickerVc.maxImagesCount;
__weak typeof(cell) weakCell = cell; __weak typeof(cell) weakCell = cell;
__weak typeof(self) weakSelf = self; __weak typeof(self) weakSelf = self;
...@@ -560,6 +564,12 @@ static CGSize AssetGridThumbnailSize; ...@@ -560,6 +564,12 @@ static CGSize AssetGridThumbnailSize;
assetModel = [models firstObject]; assetModel = [models firstObject];
[_models insertObject:assetModel atIndex:0]; [_models insertObject:assetModel atIndex:0];
} }
if (tzImagePickerVc.maxImagesCount <= 1) {
[tzImagePickerVc.selectedModels addObject:assetModel];
[self okButtonClick]; return;
}
if (tzImagePickerVc.selectedModels.count < tzImagePickerVc.maxImagesCount) { if (tzImagePickerVc.selectedModels.count < tzImagePickerVc.maxImagesCount) {
assetModel.isSelected = YES; assetModel.isSelected = YES;
[tzImagePickerVc.selectedModels addObject:assetModel]; [tzImagePickerVc.selectedModels addObject:assetModel];
......
...@@ -83,6 +83,7 @@ ...@@ -83,6 +83,7 @@
[_selectButton setImage:[UIImage imageNamedFromMyBundle:tzImagePickerVc.photoDefImageName] forState:UIControlStateNormal]; [_selectButton setImage:[UIImage imageNamedFromMyBundle:tzImagePickerVc.photoDefImageName] forState:UIControlStateNormal];
[_selectButton setImage:[UIImage imageNamedFromMyBundle:tzImagePickerVc.photoSelImageName] forState:UIControlStateSelected]; [_selectButton setImage:[UIImage imageNamedFromMyBundle:tzImagePickerVc.photoSelImageName] forState:UIControlStateSelected];
[_selectButton addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchUpInside]; [_selectButton addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchUpInside];
_selectButton.hidden = tzImagePickerVc.maxImagesCount == 1;
[_naviBar addSubview:_selectButton]; [_naviBar addSubview:_selectButton];
[_naviBar addSubview:_backButton]; [_naviBar addSubview:_backButton];
...@@ -131,7 +132,7 @@ ...@@ -131,7 +132,7 @@
_numberImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamedFromMyBundle:_tzImagePickerVc.photoNumberIconImageName]]; _numberImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamedFromMyBundle:_tzImagePickerVc.photoNumberIconImageName]];
_numberImageView.backgroundColor = [UIColor clearColor]; _numberImageView.backgroundColor = [UIColor clearColor];
_numberImageView.frame = CGRectMake(self.view.tz_width - 56 - 24, 9, 26, 26); _numberImageView.frame = CGRectMake(self.view.tz_width - 56 - 24, 9, 26, 26);
_numberImageView.hidden = _tzImagePickerVc.selectedModels.count <= 0; _numberImageView.hidden = _tzImagePickerVc.selectedModels.count <= 1;
_numberLable = [[UILabel alloc] init]; _numberLable = [[UILabel alloc] init];
_numberLable.frame = _numberImageView.frame; _numberLable.frame = _numberImageView.frame;
...@@ -139,7 +140,7 @@ ...@@ -139,7 +140,7 @@
_numberLable.textColor = [UIColor whiteColor]; _numberLable.textColor = [UIColor whiteColor];
_numberLable.textAlignment = NSTextAlignmentCenter; _numberLable.textAlignment = NSTextAlignmentCenter;
_numberLable.text = [NSString stringWithFormat:@"%zd",_tzImagePickerVc.selectedModels.count]; _numberLable.text = [NSString stringWithFormat:@"%zd",_tzImagePickerVc.selectedModels.count];
_numberLable.hidden = _tzImagePickerVc.selectedModels.count <= 0; _numberLable.hidden = _tzImagePickerVc.selectedModels.count <= 1;
_numberLable.backgroundColor = [UIColor clearColor]; _numberLable.backgroundColor = [UIColor clearColor];
[_originalPhotoButton addSubview:_originalPhotoLable]; [_originalPhotoButton addSubview:_originalPhotoLable];
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
@property (weak, nonatomic) IBOutlet UISwitch *allowPickingImageSwitch; ///< 允许选择图片 @property (weak, nonatomic) IBOutlet UISwitch *allowPickingImageSwitch; ///< 允许选择图片
@property (weak, nonatomic) IBOutlet UISwitch *allowPickingOriginalPhotoSwitch; ///< 允许选择原图 @property (weak, nonatomic) IBOutlet UISwitch *allowPickingOriginalPhotoSwitch; ///< 允许选择原图
@property (weak, nonatomic) IBOutlet UISwitch *showSheetSwitch; ///< 显示一个sheet,把拍照按钮放在外面 @property (weak, nonatomic) IBOutlet UISwitch *showSheetSwitch; ///< 显示一个sheet,把拍照按钮放在外面
@property (weak, nonatomic) IBOutlet UITextField *maxCountTF; ///< 照片最大可选张数,设置为1即为单选模式
@end @end
@implementation ViewController @implementation ViewController
...@@ -77,13 +78,14 @@ ...@@ -77,13 +78,14 @@
_layout.itemSize = CGSizeMake(_itemWH, _itemWH); _layout.itemSize = CGSizeMake(_itemWH, _itemWH);
_layout.minimumInteritemSpacing = _margin; _layout.minimumInteritemSpacing = _margin;
_layout.minimumLineSpacing = _margin; _layout.minimumLineSpacing = _margin;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 300, self.view.tz_width, self.view.tz_height - 300) collectionViewLayout:_layout]; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 344, self.view.tz_width, self.view.tz_height - 344) collectionViewLayout:_layout];
CGFloat rgb = 244 / 255.0; CGFloat rgb = 244 / 255.0;
_collectionView.alwaysBounceVertical = YES; _collectionView.alwaysBounceVertical = YES;
_collectionView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0]; _collectionView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
_collectionView.contentInset = UIEdgeInsetsMake(4, 4, 4, 4); _collectionView.contentInset = UIEdgeInsetsMake(4, 4, 4, 4);
_collectionView.dataSource = self; _collectionView.dataSource = self;
_collectionView.delegate = self; _collectionView.delegate = self;
_collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
[self.view addSubview:_collectionView]; [self.view addSubview:_collectionView];
[_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"]; [_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];
} }
...@@ -170,13 +172,18 @@ ...@@ -170,13 +172,18 @@
#pragma mark - TZImagePickerController #pragma mark - TZImagePickerController
- (void)pushImagePickerController { - (void)pushImagePickerController {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self]; if (self.maxCountTF.text.integerValue <= 0) {
return;
}
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:self.maxCountTF.text.integerValue delegate:self];
#pragma mark - 四类个性化设置,这些参数都可以不传,此时会走默认设置 #pragma mark - 四类个性化设置,这些参数都可以不传,此时会走默认设置
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto; imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
if (self.maxCountTF.text.integerValue > 1) {
// 1.如果你需要将拍照按钮放在外面,不要传这个参数 // 1.如果你需要将拍照按钮放在外面,不要传这个参数
imagePickerVc.selectedAssets = _selectedAssets; // optional, 可选的 imagePickerVc.selectedAssets = _selectedAssets; // optional, 可选的
}
imagePickerVc.allowTakePicture = self.showTakePhotoBtnSwitch.isOn; // 在内部显示拍照按钮 imagePickerVc.allowTakePicture = self.showTakePhotoBtnSwitch.isOn; // 在内部显示拍照按钮
// 2. Set the appearance // 2. Set the appearance
...@@ -382,4 +389,8 @@ ...@@ -382,4 +389,8 @@
} }
} }
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
@end @end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment