iOS基本功ios iOS开发实用技术

iOS 奇技淫巧总结

2019-05-07  本文已影响69人  Q以梦为马
  1. 利用 NSSet 的特性,NSSet 是无序、没有重复元素的数组,如果需要排序,可以考虑用 NSSortDescriptor
    NSArray *oldArray = @[@"12", @"12", @"32", @"1", @"23", @"43", @"21"];
    NSSet *set = [NSSet setWithArray:oldArray];
    NSArray *newArray = [set allObjects];
    NSLog(@"%@", newArray);
  1. 通过KVC,利用私有API,去重只需一行代码
    NSArray *oldArray = @[@"12", @"12", @"32", @"1", @"23", @"43", @"21"];
    NSArray *newArray = [oldArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
    NSLog(@"%@", newArray);
  1. 利用字典 key-value 的特性(key不能重复),将数组中的元素存入字典
    NSArray *oldArray = @[@"12", @"12", @"32", @"1", @"23", @"43", @"21"];
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
    for (NSString *tempString in oldArray) {
        [tempDict setObject:tempString forKey:tempString];
    }
    NSArray *newArray = [tempDict allValues];
    NSLog(@"%@", newArray);
    NSArray *array = @[@"12", @"23", @2, @5.2, @"4.2", @"0"];
    NSNumber *sum = [array valueForKeyPath:@"@sum.floatValue"];
    NSNumber *avg = [array valueForKeyPath:@"@avg.floatValue"];
    NSNumber *max = [array valueForKeyPath:@"@max.floatValue"];
    NSNumber *min = [array valueForKeyPath:@"@min.floatValue"];
    NSLog(@"sum = %@", sum);
    NSLog(@"avg = %@", avg);
    NSLog(@"max = %@", max);
    NSLog(@"min = %@", min);
    // 用 makeObjectsPerformSelector
    [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSArray *arr = @[@"21", @"12", @"13", @"14", @"25", @"26", @"27", @"28", @"29"];
    NSInteger index = [arr indexOfObject:@"14"
    inSortedRange:NSMakeRange(0, [arr count])
    options:NSBinarySearchingFirstEqual
    usingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 integerValue] > [obj2 integerValue]) {
        return NSOrderedDescending;
        }else if ([obj1 integerValue] < [obj2 integerValue]) {
        return NSOrderedAscending;
        }
        return NSOrderedSame;
    }];
    NSLog(@"index = %ld", index);
    [mark addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (a - b)) range:NSMakeRange(0, name.length)];

#import "UIFont+NNSize.h"
#import <objc/runtime.h>

@implementation UIFont (NNSize)

+ (void)load {
Method newMethod = class_getClassMethod([self class], @selector(nnCustomSystemFontOfSize:));
Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
method_exchangeImplementations(newMethod, method);
}

+ (UIFont *)nnCustomSystemFontOfSize:(CGFloat)fontSize {
UIFont *newFont = nil;
if ([[UIScreen mainScreen] bounds].size.height < 667.0) {
newFont = [UIFont nnCustomSystemFontOfSize:fontSize * 0.9];
} else {
newFont = [UIFont nnCustomSystemFontOfSize:fontSize];
}
return newFont;
}

@end
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}

比如:[self findHairlineImageViewUnder:self.navigationBar].hidden = YES;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.offset = scrollView.contentOffset.y;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.offset <= -200) {
        return UIStatusBarStyleDefault;
    }
    return UIStatusBarStyleLightContent;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationFade;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self setNeedsStatusBarAppearanceUpdate];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.collectionView.contentOffset.y <= -200) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationFade;
}

BOOL isView = [textView isDescendantOfView:self.view];
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
NSMutableArray *masonryLabelArray = [NSMutableArray array];
[masonryNumberLabelArray addObject:learningLabel];
[masonryNumberLabelArray addObject:rankLabel];
[masonryNumberLabelArray addObject:timeLabel];

[masonryLabelArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:10 tailSpacing:10];

[masonryNumberLabelArray mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.height.mas_equalTo(30);
}];
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSInteger integet = [self.tabBar.items indexOfObject:viewController.tabBarItem];
    [self addAnimationWithIndex:integet];
    return YES;
}

- (void)addAnimationWithIndex:(NSInteger)index {
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];
        for (UIView *tabBarButton in self.tabBar.subviews) {
            if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
            }
        }
    [NNAnimation addAnimationWithCALayer:[tabbarbuttonArray[index] layer] interval:0.1 repeatCount:2 fromValue:0.95 toValue:1.05];
}

/** 缩放动画 */
+ (void)addAnimationWithCALayer:(CALayer *)layer interval:(CFTimeInterval)interval repeatCount:(NSInteger)repeatCount fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue {
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:fromValue];
    scaleAnimation.toValue = [NSNumber numberWithFloat:toValue];
    scaleAnimation.duration = interval;
    scaleAnimation.repeatCount = repeatCount;
    scaleAnimation.autoreverses = YES;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [layer addAnimation:scaleAnimation forKey:nil];
}

设置代理 UINavigationControllerDelegate,

self.navigationController.delegate = self;

遵守代理

/** 将要显示控制器 */
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判断要显示的控制器是否是自己
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];

[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
/** 减速停止的时候开始执行 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewEndScroll:scrollView];
}

/** 停止拖拽的时候开始执行 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        [self scrollViewEndScroll:scrollView];
    }
}

- (void)scrollViewEndScroll:(UIScrollView *)scrollView {
    UITableViewCell *centerCell = [self getCenterCell];
    if (centerCell) {
        NSIndexPath *indexPath = [self indexPathForCell:centerCell];
        [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

/** 获取最中间的cell */
- (UITableViewCell *)getCenterCell {
    CGFloat minDelta = CGFLOAT_MAX;
    // 屏幕中央位置
    CGFloat screenCenterY = SCREEN_HEIGHT * 0.5;
    NSArray *cellArray = [self visibleCells];
    UITableViewCell *centerCell = nil;
    for (UITableViewCell *cell in cellArray) {
        // 获取当前 cell 的居中点坐标
        CGPoint cellCenterPoint = CGPointMake(cell.frame.origin.x, cell.frame.size.height * 0.5 + cell.frame.origin.y);
        // 转换当前的 cell 的坐标
        CGPoint coorPoint = [cell.superview convertPoint:cellCenterPoint toView:nil];
        CGFloat deltaTemp = fabs(coorPoint.y - screenCenterY);
        if (deltaTemp < minDelta) {
            centerCell = cell;
            minDelta = deltaTemp;
        }
    }
    return centerCell;
}

持续更新:iOS 奇技淫巧总结

上一篇下一篇

猜你喜欢

热点阅读