首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

iOS--喜闻乐见④

2024-12-18 来源:华拓网
  • Plain类型TableView的HeaderView和FooterView不停靠上下边缘
    大家都知道Plain的tableView头尾视图会附着在上下边缘,现在我不想让它附着了(去掉黏性),方法如下①:
    自定义HeaderView.h:
自定义HeaderView.png

HeaderView.m

HeaderView.png

自定义FooterView.h

定义FooterView.png

FooterView.m

FooterView.png

使用:

headerView使用.png footerView使用.png

方法②
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 5;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
{
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
}
else if (scrollView.contentOffset.y>=sectionHeaderHeight)
{
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}

  • 十六进制数字转颜色
    + (UIColor *)colorWithHexString:(NSString *)hexString {
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 0;
    [[NSScanner scannerWithString:[hexString substringWithRange:range]]scanHexInt:&red];
    range.location = 2;
    [[NSScanner scannerWithString:[hexString substringWithRange:range]]scanHexInt:&green];
    range.location = 4;
    [[NSScanner scannerWithString:[hexString substringWithRange:range]]scanHexInt:&blue];
    return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green / 255.0f) blue:(float)(blue / 255.0f)alpha:1.0f];
    }

  • 星星评分(传参型,支持NIB和代码)
    StarView.h

    StarView.png
    StarView.m
    - (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor clearColor];//主背景颜色为透明
    [self _creatView];
    }
    return self;
    }
    - (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];//主背景颜色为透明
    [self _creatView];
    }
    - (void)_creatView {
    //创建灰黄星星
    UIImage *yellowImg = [UIImage imageNamed:@"StarsForeground"];//灰星星图片
    UIImage *grayImg = [UIImage imageNamed:@"StarsBackground"];//黄星星图片
    _grayView = [[UIView alloc]initWithFrame:CGRectMake(0.f, 0.f, grayImg.size.width * 5.f, grayImg.size.height)];//灰星星图片frame
    _grayView.backgroundColor = [UIColor colorWithPatternImage:grayImg];//灰星星图片背景颜色
    [self addSubview:_grayView];//加载灰星星图片
    _yellowView= [[UIView alloc]initWithFrame:CGRectMake(0.f, 0.f, yellowImg.size.width * 5.f, yellowImg.size.height)];//黄星星图片frame
    _yellowView.backgroundColor = [UIColor colorWithPatternImage:yellowImg];//黄星星图片背景颜色
    [self addSubview:_yellowView];//加载黄星星图片
    // 计算缩放倍数
    CGFloat scale = self.frame.size.height / yellowImg.size.height;
    //放大倍数
    CGAffineTransform t = CGAffineTransformMakeScale(scale, scale);//高宽同比例缩放
    _grayView.transform = t;
    _yellowView.transform = t;
    //星星视图修改transform后,坐标会被改变,重新恢复坐标
    _yellowView.origin = CGPointZero;
    _grayView.origin = CGPointZero;
    }
    - (void)setRating:(CGFloat)rating {
    if (_rating != rating) {
    _rating = rating;
    CGFloat s = [self backFloatWithRatingFloat:_rating];
    CGFloat yellowWidth = s * self.frame.size.width;
    _yellowView.width = yellowWidth;//将比例后宽度设置为黄星星宽度
    }
    }
    //大于等于N.5的按N+1算,小于N.f按大于N按N.5算,等于N按N算(0 < N <5)
    - (CGFloat) backFloatWithRatingFloat:(CGFloat)ratingFloat {
    if (ratingFloat == floorf(ratingFloat)) {
    return floorf(ratingFloat) / 5.f;
    }else if (ratingFloat > floorf(ratingFloat) && ratingFloat <= (floorf(ratingFloat) + 0.5)) {
    return (floorf(ratingFloat) + 0.5f) / 5.f;
    }else {
    return (floorf(ratingFloat) + 1.f) / 5.f;
    }
    }
    - (void)layoutSubviews {
    [super layoutSubviews];
    //创建灰黄星星
    UIImage *yellowImg = [UIImage imageNamed:@"StarsForeground"];//灰星星图片
    UIImage *grayImg = [UIImage imageNamed:@"StarsBackground"];//黄星星图片
    _grayView.backgroundColor = [UIColor colorWithPatternImage:grayImg];//灰星星图片背景颜色
    _yellowView.backgroundColor = [UIColor colorWithPatternImage:yellowImg];//黄星星图片背景颜色
    }

PS:评分为五星满分,可自行修改成自己的需求(backFloatWithRatingFloat:)

  • 查看ipa支持版本(32位,64位)
    取出ipa的执行文件,在终端输入otool -vh ipa执行文件路径

  • 显示/隐藏文件
    //显示
    defaults write com.apple.finder AppleShowAllFiles -bool true
    killall Finder
    //隐藏
    defaults write com.apple.finder AppleShowAllFiles -bool false
    killall Finder

  • 查看工程量
    find . -name ".m" -or -name ".h" -or -name ".xib" -or -name ".c" |xargs wc -l

  • 获取iOS设备的型号
    +(NSString*)deviceString {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([deviceString isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"]) return @"iPhone 4 GSM";
    if ([deviceString isEqualToString:@"iPhone3,2"]) return @"iPhone 4 WCDMA";
    if ([deviceString isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPad1,1"]) return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"i386"]) return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"]) return @"Simulator";
    NSLog(@"NOTE: Unknown device type: %@", deviceString); return deviceString;
    }
    //获取系统版本
    NSLog([[UIDevice currentDevice] name]); // Name of the phone as named by u
    NSLog([[UIDevice currentDevice] uniqueIdentifier]); // A GUID like string
    NSLog([[UIDevice currentDevice] systemName]); // "iPhone OS"
    NSLog([[UIDevice currentDevice] systemVersion]); // "2.2.1"
    NSLog([[UIDevice currentDevice] model]); // "iPhone" on both devices
    NSLog([[UIDevice currentDevice] localizedModel]); // "iPhone" on both devices
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];

补全

获取系统版本.png
显示全文