cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/QMUIKit/QMUIComponents/QMUIMoreOperationController.m

743 lines
33 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// QMUIMoreOperationController.m
// qmui
//
// Created by QMUI Team on 17/11/15.
//
#import "QMUIMoreOperationController.h"
#import "QMUICore.h"
#import "CALayer+QMUI.h"
#import "UIControl+QMUI.h"
#import "UIView+QMUI.h"
#import "NSArray+QMUI.h"
#import "UIScrollView+QMUI.h"
#import "QMUILog.h"
#import "QMUIAppearance.h"
static NSInteger const kQMUIMoreOperationItemViewTagOffset = 999;
@interface QMUIMoreOperationItemView () {
NSInteger _tag;
}
@property(nonatomic, weak) QMUIMoreOperationController *moreOperationController;
@property(nonatomic, copy) void (^handler)(QMUIMoreOperationController *moreOperationController, QMUIMoreOperationItemView *itemView);
// QMUIMoreOperationController itemView moreOperationController
// @param moreOperationController 使 [QMUIMoreOperationController appearance]
- (void)formatItemViewStyleWithMoreOperationController:(QMUIMoreOperationController *)moreOperationController;
@end
@implementation QMUIMoreOperationController (UIAppearance)
+ (instancetype)appearance {
return [QMUIAppearance appearanceForClass:self];
}
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self initAppearance];
});
}
+ (void)initAppearance {
QMUIMoreOperationController *moreOperationViewControllerAppearance = QMUIMoreOperationController.appearance;
moreOperationViewControllerAppearance.contentBackgroundColor = UIColorForBackground;
moreOperationViewControllerAppearance.contentEdgeMargins = UIEdgeInsetsMake(0, 10, 10, 10);
moreOperationViewControllerAppearance.contentMaximumWidth = [QMUIHelper screenSizeFor55Inch].width - UIEdgeInsetsGetHorizontalValue(moreOperationViewControllerAppearance.contentEdgeMargins);
moreOperationViewControllerAppearance.contentCornerRadius = 10;
moreOperationViewControllerAppearance.contentPaddings = UIEdgeInsetsMake(10, 0, 5, 0);
moreOperationViewControllerAppearance.scrollViewSeparatorColor = UIColorMakeWithRGBA(0, 0, 0, .15f);
moreOperationViewControllerAppearance.scrollViewContentInsets = UIEdgeInsetsMake(14, 8, 14, 8);
moreOperationViewControllerAppearance.itemBackgroundColor = UIColorClear;
moreOperationViewControllerAppearance.itemTitleColor = UIColorGrayDarken;
moreOperationViewControllerAppearance.itemTitleFont = UIFontMake(11);
moreOperationViewControllerAppearance.itemPaddingHorizontal = 16;
moreOperationViewControllerAppearance.itemTitleMarginTop = 9;
moreOperationViewControllerAppearance.itemMinimumMarginHorizontal = 0;
moreOperationViewControllerAppearance.automaticallyAdjustItemMargins = YES;
moreOperationViewControllerAppearance.cancelButtonBackgroundColor = UIColorForBackground;
moreOperationViewControllerAppearance.cancelButtonTitleColor = UIColorBlue;
moreOperationViewControllerAppearance.cancelButtonSeparatorColor = UIColorMakeWithRGBA(0, 0, 0, .15f);
moreOperationViewControllerAppearance.cancelButtonFont = UIFontBoldMake(16);
moreOperationViewControllerAppearance.cancelButtonHeight = 56.0;
moreOperationViewControllerAppearance.cancelButtonMarginTop = 0;
moreOperationViewControllerAppearance.isExtendBottomLayout = NO;
}
@end
@interface QMUIMoreOperationController ()
@property(nonatomic, strong) NSMutableArray<UIScrollView *> *mutableScrollViews;
@property(nonatomic, strong) NSMutableArray<NSMutableArray<QMUIMoreOperationItemView *> *> *mutableItems;
@property(nonatomic, strong) CALayer *extendLayer;
@property(nonatomic, assign, getter=isShowing, readwrite) BOOL showing;
@property(nonatomic, assign, getter=isAnimating, readwrite) BOOL animating;
@property(nonatomic, assign) BOOL hideByCancel; // NO
@end
@implementation QMUIMoreOperationController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self didInitialize];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self didInitialize];
}
return self;
}
- (void)didInitialize {
[self qmui_applyAppearance];
self.mutableScrollViews = [[NSMutableArray alloc] init];
self.mutableItems = [[NSMutableArray alloc] init];
}
#pragma mark - Getters & Setters
@synthesize contentView = _contentView;
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = self.contentBackgroundColor;
}
return _contentView;
}
@synthesize cancelButton = _cancelButton;
- (QMUIButton *)cancelButton {
if (!_cancelButton) {
_cancelButton = [[QMUIButton alloc] init];
_cancelButton.qmui_automaticallyAdjustTouchHighlightedInScrollView = YES;
_cancelButton.adjustsButtonWhenHighlighted = NO;
_cancelButton.titleLabel.font = self.cancelButtonFont;
_cancelButton.backgroundColor = self.cancelButtonBackgroundColor;
[_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[_cancelButton setTitleColor:self.cancelButtonTitleColor forState:UIControlStateNormal];
[_cancelButton setTitleColor:[self.cancelButtonTitleColor colorWithAlphaComponent:ButtonHighlightedAlpha] forState:UIControlStateHighlighted];
_cancelButton.qmui_borderPosition = self.cancelButtonMarginTop > 0 ? QMUIViewBorderPositionNone : QMUIViewBorderPositionTop;
_cancelButton.qmui_borderColor = self.cancelButtonSeparatorColor;
[_cancelButton addTarget:self action:@selector(handleCancelButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.contentView];
[self.view addSubview:self.cancelButton];
self.extendLayer = [CALayer layer];
self.extendLayer.hidden = !self.isExtendBottomLayout;
[self.extendLayer qmui_removeDefaultAnimations];
[self.view.layer addSublayer:self.extendLayer];
[self updateExtendLayerAppearance];
[self updateCornerRadius];
}
- (NSArray<UIScrollView *> *)scrollViews {
return [self.mutableScrollViews copy];
}
#pragma mark - Layout
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
__block CGFloat layoutY = CGRectGetHeight(self.view.bounds);
if (!self.extendLayer.hidden) {
self.extendLayer.frame = CGRectMake(0, layoutY, CGRectGetWidth(self.view.bounds), SafeAreaInsetsConstantForDeviceWithNotch.bottom);
if (self.view.clipsToBounds) {
QMUILog(@"QMUIMoreOperationController", @"%@ 需要显示 extendLayer但却被父级 clip 掉了,可能看不到", NSStringFromClass(self.class));
}
}
BOOL isCancelButtonShowing = !self.cancelButton.hidden;
if (isCancelButtonShowing) {
self.cancelButton.frame = CGRectMake(0, layoutY - self.cancelButtonHeight, CGRectGetWidth(self.view.bounds), self.cancelButtonHeight);
[self.cancelButton setNeedsLayout];
layoutY = CGRectGetMinY(self.cancelButton.frame) - self.cancelButtonMarginTop;
}
self.contentView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), layoutY);
layoutY = self.contentPaddings.top;
CGFloat contentWidth = CGRectGetWidth(self.contentView.bounds) - UIEdgeInsetsGetHorizontalValue(self.contentPaddings);
[self.mutableScrollViews enumerateObjectsUsingBlock:^(UIScrollView * _Nonnull scrollView, NSUInteger idx, BOOL * _Nonnull stop) {
scrollView.frame = CGRectMake(self.contentPaddings.left, layoutY, contentWidth, CGRectGetHeight(scrollView.frame));
// safeAreaInsets 使 scrollView.safeAreaInsets scrollView safeAreaInsets 0 scrollView.superview.safeAreaInsets 使 scrollView.superview self.view
// insets
// UIEdgeInsets scrollViewSafeAreaInsets = scrollView.safeAreaInsets;
UIEdgeInsets scrollViewSafeAreaInsets = UIEdgeInsetsMake(fmax(self.view.safeAreaInsets.top - scrollView.qmui_top, 0), fmax(self.view.safeAreaInsets.left - scrollView.qmui_left, 0), 0, fmax(self.view.safeAreaInsets.right - (self.view.qmui_width - scrollView.qmui_right), 0));
NSArray<QMUIMoreOperationItemView *> *itemSection = self.mutableItems[idx];
QMUIMoreOperationItemView *exampleItemView = itemSection.firstObject;
CGFloat exampleItemWidth = exampleItemView.imageView.image.size.width + self.itemPaddingHorizontal * 2;
CGFloat scrollViewVisibleWidth = contentWidth - scrollView.contentInset.left - scrollViewSafeAreaInsets.left;// contentInset.right
CGFloat columnCount = (scrollViewVisibleWidth + self.itemMinimumMarginHorizontal) / (exampleItemWidth + self.itemMinimumMarginHorizontal);
// scrollView item
if (self.automaticallyAdjustItemMargins) {
columnCount = [self suitableColumnCountWithCount:columnCount];
}
CGFloat finalItemMarginHorizontal = flat((scrollViewVisibleWidth - exampleItemWidth * columnCount) / columnCount);
__block CGFloat maximumItemHeight = 0;
__block CGFloat itemViewMinX = scrollViewSafeAreaInsets.left;
[itemSection enumerateObjectsUsingBlock:^(QMUIMoreOperationItemView * _Nonnull itemView, NSUInteger idx, BOOL * _Nonnull stop) {
CGSize itemSize = CGSizeFlatted([itemView sizeThatFits:CGSizeMake(exampleItemWidth, CGFLOAT_MAX)]);
maximumItemHeight = fmax(maximumItemHeight, itemSize.height);
itemView.frame = CGRectMake(itemViewMinX, 0, exampleItemWidth, itemSize.height);
itemViewMinX = CGRectGetMaxX(itemView.frame) + finalItemMarginHorizontal;
}];
scrollView.contentSize = CGSizeMake(itemViewMinX - finalItemMarginHorizontal + scrollViewSafeAreaInsets.right, maximumItemHeight);
scrollView.frame = CGRectSetHeight(scrollView.frame, scrollView.contentSize.height + UIEdgeInsetsGetVerticalValue(scrollView.contentInset));
layoutY = CGRectGetMaxY(scrollView.frame);
}];
}
- (CGFloat)suitableColumnCountWithCount:(CGFloat)columnCount {
// item 3.6 3.53.2 2.5
CGFloat result = round(columnCount) - .5;;
return result;
}
- (void)showFromBottom {
if (self.showing || self.animating) {
return;
}
self.hideByCancel = YES;
__weak __typeof(self)weakSelf = self;
QMUIModalPresentationViewController *modalPresentationViewController = [[QMUIModalPresentationViewController alloc] init];
modalPresentationViewController.delegate = self;
modalPresentationViewController.maximumContentViewWidth = self.contentMaximumWidth;
modalPresentationViewController.contentViewMargins = self.contentEdgeMargins;
modalPresentationViewController.contentViewController = self;
__weak __typeof(modalPresentationViewController)weakModalController = modalPresentationViewController;
modalPresentationViewController.layoutBlock = ^(CGRect containerBounds, CGFloat keyboardHeight, CGRect contentViewDefaultFrame) {
weakModalController.contentView.qmui_frameApplyTransform = CGRectSetY(contentViewDefaultFrame, CGRectGetHeight(containerBounds) - weakModalController.contentViewMargins.bottom - CGRectGetHeight(contentViewDefaultFrame) - weakModalController.view.safeAreaInsets.bottom);
};
modalPresentationViewController.showingAnimation = ^(UIView *dimmingView, CGRect containerBounds, CGFloat keyboardHeight, CGRect contentViewFrame, void(^completion)(BOOL finished)) {
if ([weakSelf.delegate respondsToSelector:@selector(willPresentMoreOperationController:)]) {
[weakSelf.delegate willPresentMoreOperationController:weakSelf];
}
dimmingView.alpha = 0;
weakModalController.contentView.frame = CGRectSetY(contentViewFrame, CGRectGetHeight(containerBounds));
[UIView animateWithDuration:.25 delay:0.0 options:QMUIViewAnimationOptionsCurveOut animations:^(void) {
dimmingView.alpha = 1;
weakModalController.contentView.frame = contentViewFrame;
} completion:^(BOOL finished) {
weakSelf.showing = YES;
weakSelf.animating = NO;
if ([weakSelf.delegate respondsToSelector:@selector(didPresentMoreOperationController:)]) {
[weakSelf.delegate didPresentMoreOperationController:weakSelf];
}
if (completion) {
completion(finished);
}
}];
};
modalPresentationViewController.hidingAnimation = ^(UIView *dimmingView, CGRect containerBounds, CGFloat keyboardHeight, void(^completion)(BOOL finished)) {
[UIView animateWithDuration:.25 delay:0.0 options:QMUIViewAnimationOptionsCurveOut animations:^(void) {
dimmingView.alpha = 0;
weakModalController.contentView.frame = CGRectSetY(weakModalController.contentView.frame, CGRectGetHeight(containerBounds));
} completion:^(BOOL finished) {
if (completion) {
completion(finished);
}
}];
};
self.animating = YES;
[modalPresentationViewController showWithAnimated:YES completion:NULL];
}
- (void)hideToBottom {
if (!self.showing || self.animating) {
return;
}
self.hideByCancel = NO;
[self.qmui_modalPresentationViewController hideWithAnimated:YES completion:NULL];
}
#pragma mark - Item
- (void)setItems:(NSArray<NSArray<QMUIMoreOperationItemView *> *> *)items {
[self.mutableItems qmui_enumerateNestedArrayWithBlock:^(QMUIMoreOperationItemView *itemView, BOOL *stop) {
[itemView removeFromSuperview];
}];
[self.mutableItems removeAllObjects];
self.mutableItems = [items qmui_mutableCopyNestedArray];
[self.mutableScrollViews enumerateObjectsUsingBlock:^(UIScrollView * _Nonnull scrollView, NSUInteger idx, BOOL * _Nonnull stop) {
[scrollView removeFromSuperview];
}];
[self.mutableScrollViews removeAllObjects];
[self.mutableItems enumerateObjectsUsingBlock:^(NSArray<QMUIMoreOperationItemView *> * _Nonnull itemViewSection, NSUInteger scrollViewIndex, BOOL * _Nonnull stop) {
UIScrollView *scrollView = [self addScrollViewAtIndex:scrollViewIndex];
[itemViewSection enumerateObjectsUsingBlock:^(QMUIMoreOperationItemView * _Nonnull itemView, NSUInteger itemViewIndex, BOOL * _Nonnull stop) {
[self addItemView:itemView toScrollView:scrollView];
}];
}];
[self setViewNeedsLayoutIfLoaded];
}
- (NSArray<NSArray<QMUIMoreOperationItemView *> *> *)items {
return [self.mutableItems copy];
}
- (void)addItemView:(QMUIMoreOperationItemView *)itemView inSection:(NSInteger)section {
if (section == self.mutableItems.count) {
// itemView section
[self.mutableItems addObject:[@[itemView] mutableCopy]];
} else {
[self.mutableItems[section] addObject:itemView];
}
itemView.moreOperationController = self;
if (section == self.mutableScrollViews.count) {
// section
[self addScrollViewAtIndex:section];
}
if (section < self.mutableScrollViews.count) {
[self addItemView:itemView toScrollView:self.mutableScrollViews[section]];
}
[self setViewNeedsLayoutIfLoaded];
}
- (void)insertItemView:(QMUIMoreOperationItemView *)itemView atIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == self.mutableItems.count) {
// itemView section
[self.mutableItems addObject:[@[itemView] mutableCopy]];
} else {
[self.mutableItems[indexPath.section] insertObject:itemView atIndex:indexPath.item];
}
itemView.moreOperationController = self;
if (indexPath.section == self.mutableScrollViews.count) {
// section
[self addScrollViewAtIndex:indexPath.section];
}
if (indexPath.section < self.mutableScrollViews.count) {
[itemView formatItemViewStyleWithMoreOperationController:self];
[self.mutableScrollViews[indexPath.section] insertSubview:itemView atIndex:indexPath.item];
}
[self setViewNeedsLayoutIfLoaded];
}
- (void)removeItemViewAtIndexPath:(NSIndexPath *)indexPath {
QMUIMoreOperationItemView *itemView = self.mutableScrollViews[indexPath.section].subviews[indexPath.item];
itemView.moreOperationController = nil;
[itemView removeFromSuperview];
NSMutableArray<QMUIMoreOperationItemView *> *itemViewSection = self.mutableItems[indexPath.section];
[itemViewSection removeObject:itemView];
if (itemViewSection.count == 0) {
[self.mutableItems removeObject:itemViewSection];
[self.mutableScrollViews[indexPath.section] removeFromSuperview];
[self.mutableScrollViews removeObjectAtIndex:indexPath.section];
[self updateScrollViewsBorderStyle];
}
[self setViewNeedsLayoutIfLoaded];
}
- (QMUIMoreOperationItemView *)itemViewWithTag:(NSInteger)tag {
__block QMUIMoreOperationItemView *result = nil;
[self.mutableItems qmui_enumerateNestedArrayWithBlock:^(QMUIMoreOperationItemView *itemView, BOOL *stop) {
if (itemView.tag == tag) {
result = itemView;
*stop = YES;
}
}];
return result;
}
- (NSIndexPath *)indexPathWithItemView:(QMUIMoreOperationItemView *)itemView {
for (NSInteger section = 0; section < self.mutableItems.count; section++) {
NSInteger index = [self.mutableItems[section] indexOfObject:itemView];
if (index != NSNotFound) {
return [NSIndexPath indexPathForItem:index inSection:section];
}
}
return nil;
}
- (UIScrollView *)addScrollViewAtIndex:(NSInteger)index {
UIScrollView *scrollView = [self generateScrollViewWithIndex:index];
[self.contentView addSubview:scrollView];
[self.mutableScrollViews addObject:scrollView];
[self updateScrollViewsBorderStyle];
return scrollView;
}
- (void)addItemView:(QMUIMoreOperationItemView *)itemView toScrollView:(UIScrollView *)scrollView {
[itemView formatItemViewStyleWithMoreOperationController:self];
[scrollView addSubview:itemView];
}
- (UIScrollView *)generateScrollViewWithIndex:(NSInteger)index {
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.alwaysBounceHorizontal = YES;
scrollView.qmui_borderColor = self.scrollViewSeparatorColor;
scrollView.qmui_borderPosition = (self.scrollViewSeparatorColor && index != 0) ? QMUIViewBorderPositionTop : QMUIViewBorderPositionNone;
scrollView.scrollsToTop = NO;
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
scrollView.contentInset = self.scrollViewContentInsets;
[scrollView qmui_scrollToTopForce:YES animated:NO];
return scrollView;
}
- (void)updateScrollViewsBorderStyle {
[self.mutableScrollViews enumerateObjectsUsingBlock:^(UIScrollView * _Nonnull scrollView, NSUInteger idx, BOOL * _Nonnull stop) {
scrollView.qmui_borderColor = self.scrollViewSeparatorColor;
scrollView.qmui_borderPosition = idx != 0 ? QMUIViewBorderPositionTop : QMUIViewBorderPositionNone;
}];
}
#pragma mark - Event
- (void)handleCancelButtonEvent:(id)sender {
if (!self.showing || self.animating) {
return;
}
[self.qmui_modalPresentationViewController hideWithAnimated:YES completion:NULL];
}
- (void)handleItemViewEvent:(QMUIMoreOperationItemView *)itemView {
if ([self.delegate respondsToSelector:@selector(moreOperationController:didSelectItemView:)]) {
[self.delegate moreOperationController:self didSelectItemView:itemView];
}
if (itemView.handler) {
itemView.handler(self, itemView);
}
}
#pragma mark - Property setter
- (void)setContentBackgroundColor:(UIColor *)contentBackgroundColor {
_contentBackgroundColor = contentBackgroundColor;
_contentView.backgroundColor = contentBackgroundColor;
}
- (void)setScrollViewSeparatorColor:(UIColor *)scrollViewSeparatorColor {
_scrollViewSeparatorColor = scrollViewSeparatorColor;
[self updateScrollViewsBorderStyle];
}
- (void)setScrollViewContentInsets:(UIEdgeInsets)scrollViewContentInsets {
_scrollViewContentInsets = scrollViewContentInsets;
if (self.mutableScrollViews) {
for (UIScrollView *scrollView in self.mutableScrollViews) {
scrollView.contentInset = scrollViewContentInsets;
}
[self setViewNeedsLayoutIfLoaded];
}
}
- (void)setCancelButtonBackgroundColor:(UIColor *)cancelButtonBackgroundColor {
_cancelButtonBackgroundColor = cancelButtonBackgroundColor;
_cancelButton.backgroundColor = cancelButtonBackgroundColor;
[self updateExtendLayerAppearance];
}
- (void)setCancelButtonTitleColor:(UIColor *)cancelButtonTitleColor {
_cancelButtonTitleColor = cancelButtonTitleColor;
if (_cancelButton) {
[_cancelButton setTitleColor:cancelButtonTitleColor forState:UIControlStateNormal];
[_cancelButton setTitleColor:[cancelButtonTitleColor colorWithAlphaComponent:ButtonHighlightedAlpha] forState:UIControlStateHighlighted];
}
}
- (void)setCancelButtonSeparatorColor:(UIColor *)cancelButtonSeparatorColor {
_cancelButtonSeparatorColor = cancelButtonSeparatorColor;
_cancelButton.qmui_borderColor = cancelButtonSeparatorColor;
}
- (void)setItemBackgroundColor:(UIColor *)itemBackgroundColor {
_itemBackgroundColor = itemBackgroundColor;
[self.mutableItems qmui_enumerateNestedArrayWithBlock:^(QMUIMoreOperationItemView *itemView, BOOL *stop) {
itemView.imageView.backgroundColor = itemBackgroundColor;
}];
}
- (void)setItemTitleColor:(UIColor *)itemTitleColor {
_itemTitleColor = itemTitleColor;
[self.mutableItems qmui_enumerateNestedArrayWithBlock:^(QMUIMoreOperationItemView *itemView, BOOL *stop) {
[itemView setTitleColor:itemTitleColor forState:UIControlStateNormal];
}];
}
- (void)setItemTitleFont:(UIFont *)itemTitleFont {
_itemTitleFont = itemTitleFont;
[self.mutableItems qmui_enumerateNestedArrayWithBlock:^(QMUIMoreOperationItemView *itemView, BOOL *stop) {
itemView.titleLabel.font = itemTitleFont;
[itemView setNeedsLayout];
}];
}
- (void)setItemPaddingHorizontal:(CGFloat)itemPaddingHorizontal {
_itemPaddingHorizontal = itemPaddingHorizontal;
[self setViewNeedsLayoutIfLoaded];
}
- (void)setItemTitleMarginTop:(CGFloat)itemTitleMarginTop {
_itemTitleMarginTop = itemTitleMarginTop;
[self.mutableItems qmui_enumerateNestedArrayWithBlock:^(QMUIMoreOperationItemView *itemView, BOOL *stop) {
itemView.titleEdgeInsets = UIEdgeInsetsMake(itemTitleMarginTop, 0, 0, 0);
[itemView setNeedsLayout];
}];
}
- (void)setItemMinimumMarginHorizontal:(CGFloat)itemMinimumMarginHorizontal {
_itemMinimumMarginHorizontal = itemMinimumMarginHorizontal;
[self setViewNeedsLayoutIfLoaded];
}
- (void)setAutomaticallyAdjustItemMargins:(BOOL)automaticallyAdjustItemMargins {
_automaticallyAdjustItemMargins = automaticallyAdjustItemMargins;
[self setViewNeedsLayoutIfLoaded];
}
- (void)setCancelButtonFont:(UIFont *)cancelButtonFont {
_cancelButtonFont = cancelButtonFont;
_cancelButton.titleLabel.font = cancelButtonFont;
[_cancelButton setNeedsLayout];
}
- (void)setContentCornerRadius:(CGFloat)contentCornerRadius {
_contentCornerRadius = contentCornerRadius;
[self updateCornerRadius];
}
- (void)setCancelButtonMarginTop:(CGFloat)cancelButtonMarginTop {
_cancelButtonMarginTop = cancelButtonMarginTop;
_cancelButton.qmui_borderPosition = cancelButtonMarginTop > 0 ? QMUIViewBorderPositionNone : QMUIViewBorderPositionTop;
[self updateCornerRadius];
[self setViewNeedsLayoutIfLoaded];
}
- (void)setIsExtendBottomLayout:(BOOL)isExtendBottomLayout {
_isExtendBottomLayout = isExtendBottomLayout;
if (isExtendBottomLayout) {
self.extendLayer.hidden = NO;
[self updateExtendLayerAppearance];
} else {
self.extendLayer.hidden = YES;
}
}
- (void)setViewNeedsLayoutIfLoaded {
if (self.isShowing) {
[self.qmui_modalPresentationViewController updateLayout];
[self.view setNeedsLayout];
} else if ([self isViewLoaded]) {
[self.view setNeedsLayout];
}
}
- (void)updateExtendLayerAppearance {
self.extendLayer.backgroundColor = self.cancelButtonBackgroundColor.CGColor;
}
- (void)updateCornerRadius {
if (self.cancelButtonMarginTop > 0) {
if (self.isViewLoaded) {
self.view.layer.cornerRadius = 0;
self.view.clipsToBounds = NO;
}
_contentView.layer.cornerRadius = self.contentCornerRadius;
_cancelButton.layer.cornerRadius = self.contentCornerRadius;
} else {
if (self.isViewLoaded) {
self.view.layer.cornerRadius = self.contentCornerRadius;
self.view.clipsToBounds = self.view.layer.cornerRadius > 0;// clip
}
_contentView.layer.cornerRadius = 0;
_cancelButton.layer.cornerRadius = 0;
}
}
#pragma mark - <QMUIModalPresentationContentViewControllerProtocol>
- (CGSize)preferredContentSizeInModalPresentationViewController:(QMUIModalPresentationViewController *)controller keyboardHeight:(CGFloat)keyboardHeight limitSize:(CGSize)limitSize {
__block CGFloat contentHeight = (self.cancelButton.hidden ? 0 : self.cancelButtonHeight + self.cancelButtonMarginTop);
[self.mutableScrollViews enumerateObjectsUsingBlock:^(UIScrollView * _Nonnull scrollView, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray<QMUIMoreOperationItemView *> *itemSection = self.mutableItems[idx];
QMUIMoreOperationItemView *exampleItemView = itemSection.firstObject;
CGFloat exampleItemWidth = exampleItemView.imageView.image.size.width + self.itemPaddingHorizontal * 2;
__block CGFloat maximumItemHeight = 0;
[itemSection enumerateObjectsUsingBlock:^(QMUIMoreOperationItemView * _Nonnull itemView, NSUInteger idx, BOOL * _Nonnull stop) {
CGSize itemSize = CGSizeFlatted([itemView sizeThatFits:CGSizeMake(exampleItemWidth, CGFLOAT_MAX)]);
maximumItemHeight = fmax(maximumItemHeight, itemSize.height);
}];
contentHeight += maximumItemHeight + UIEdgeInsetsGetVerticalValue(scrollView.contentInset);
}];
if (self.mutableScrollViews.count) {
contentHeight += UIEdgeInsetsGetVerticalValue(self.contentPaddings);
}
limitSize.height = contentHeight;
return limitSize;
}
#pragma mark - <QMUIModalPresentationViewControllerDelegate>
- (void)willHideModalPresentationViewController:(QMUIModalPresentationViewController *)controller {
self.animating = YES;
if ([self.delegate respondsToSelector:@selector(willDismissMoreOperationController:cancelled:)]) {
[self.delegate willDismissMoreOperationController:self cancelled:self.hideByCancel];
}
}
- (void)didHideModalPresentationViewController:(QMUIModalPresentationViewController *)controller {
self.showing = NO;
self.animating = NO;
if ([self.delegate respondsToSelector:@selector(didDismissMoreOperationController:cancelled:)]) {
[self.delegate didDismissMoreOperationController:self cancelled:self.hideByCancel];
}
}
#pragma mark - <QMUIModalPresentationComponentProtocol>
- (void)hideModalPresentationComponent {
[self hideToBottom];
}
@end
@implementation QMUIMoreOperationItemView
@dynamic tag;
+ (instancetype)itemViewWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title selectedTitle:(NSString *)selectedTitle handler:(void (^)(QMUIMoreOperationController *, QMUIMoreOperationItemView *))handler {
QMUIMoreOperationItemView *itemView = [[self alloc] init];
[itemView setImage:image forState:UIControlStateNormal];
[itemView setImage:selectedImage forState:UIControlStateSelected];
[itemView setImage:selectedImage forState:UIControlStateHighlighted|UIControlStateSelected];
[itemView setTitle:title forState:UIControlStateNormal];
[itemView setTitle:selectedTitle forState:UIControlStateHighlighted|UIControlStateSelected];
[itemView setTitle:selectedTitle forState:UIControlStateSelected];
itemView.handler = handler;
[itemView formatItemViewStyleWithMoreOperationController:nil];
return itemView;
}
+ (instancetype)itemViewWithImage:(UIImage *)image title:(NSString *)title handler:(void (^)(QMUIMoreOperationController *, QMUIMoreOperationItemView *))handler {
return [self itemViewWithImage:image selectedImage:nil title:title selectedTitle:nil handler:handler];
}
+ (instancetype)itemViewWithImage:(UIImage *)image
title:(NSString *)title
tag:(NSInteger)tag
handler:(void (^)(QMUIMoreOperationController *moreOperationController, QMUIMoreOperationItemView *itemView))handler {
QMUIMoreOperationItemView *itemView = [self itemViewWithImage:image title:title handler:handler];
itemView.tag = tag;
return itemView;
}
+ (instancetype)itemViewWithImage:(UIImage *)image
selectedImage:(UIImage *)selectedImage
title:(NSString *)title
selectedTitle:(NSString *)selectedTitle
tag:(NSInteger)tag
handler:(void (^)(QMUIMoreOperationController *moreOperationController, QMUIMoreOperationItemView *itemView))handler {
QMUIMoreOperationItemView *itemView = [self itemViewWithImage:image selectedImage:selectedImage title:title selectedTitle:selectedTitle handler:handler];
itemView.tag = tag;
return itemView;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.imagePosition = QMUIButtonImagePositionTop;
self.adjustsButtonWhenHighlighted = NO;
self.qmui_automaticallyAdjustTouchHighlightedInScrollView = YES;
self.titleLabel.numberOfLines = 0;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.contentMode = UIViewContentModeCenter;
}
return self;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
self.imageView.alpha = highlighted ? ButtonHighlightedAlpha : 1;
}
// QMUIMoreOperationController itemView itemView
- (void)formatItemViewStyleWithMoreOperationController:(QMUIMoreOperationController *)moreOperationController {
if (moreOperationController) {
// controller 便 delegate
[self addTarget:moreOperationController action:@selector(handleItemViewEvent:) forControlEvents:UIControlEventTouchUpInside];
} else {
// nil 使 appearance
moreOperationController = [QMUIMoreOperationController appearance];
}
self.titleLabel.font = moreOperationController.itemTitleFont;
self.titleEdgeInsets = UIEdgeInsetsMake(moreOperationController.itemTitleMarginTop, 0, 0, 0);
[self setTitleColor:moreOperationController.itemTitleColor forState:UIControlStateNormal];
self.imageView.backgroundColor = moreOperationController.itemBackgroundColor;
}
- (void)setTag:(NSInteger)tag {
_tag = tag + kQMUIMoreOperationItemViewTagOffset;
}
- (NSInteger)tag {
return MAX(-1, _tag - kQMUIMoreOperationItemViewTagOffset);// -10 itemView tag: itemView.tag 0 itemView tag: itemView.tag 0 tag 0 -1 使 tag: itemView
}
- (NSIndexPath *)indexPath {
if (self.moreOperationController) {
return [self.moreOperationController indexPathWithItemView:self];
}
return nil;
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@:\t%p\nimage:\t\t\t%@\nselectedImage:\t%@\ntitle:\t\t\t%@\nselectedTitle:\t%@\nindexPath:\t\t%@\ntag:\t\t\t\t%@", NSStringFromClass(self.class), self, [self imageForState:UIControlStateNormal], [self imageForState:UIControlStateSelected] == [self imageForState:UIControlStateNormal] ? nil : [self imageForState:UIControlStateSelected], [self titleForState:UIControlStateNormal], [self titleForState:UIControlStateSelected] == [self titleForState:UIControlStateNormal] ? nil : [self titleForState:UIControlStateSelected], ({self.indexPath ? [NSString stringWithFormat:@"%@ - %@", @(self.indexPath.section), @(self.indexPath.item)] : nil;}), @(self.tag)];
}
@end