iOS 实用功能汇总(1)

    xiaoxiao2021-04-12  35

    1.去掉tableview header的黏性

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self.searchBar resignFirstResponder]; if (scrollView == self.listTableView) { CGFloat sectionHeaderHeight = HEADVIEW_HIGHT; 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); } } }

    2.拖动tableView时收起键盘

    只有一行代码: tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 其中keyboardDismissMode是UIScrollView的属性, 它的值除了UIScrollViewKeyboardDismissModeNone, 还有一种是UIScrollViewKeyboardDismissModeInteractive, 表示键盘可以随着手指下滑而移出屏幕 既然是UIScrollView的属性,那么在文字常用的UITextView等控件中也可以使用.

    3.获取某个view所在的控制器

    - (UIViewController *)viewController { UIViewController *viewController = nil; UIResponder *next = self.nextResponder; while (next) { if ([next isKindOfClass:[UIViewController class]]) { viewController = (UIViewController *)next; break; } next = next.nextResponder; } return viewController; }

    4.获取图片某一点的颜色

    - (UIColor*) getPixelColorAtLocation:(CGPoint)pointinImage:(UIImage *)image { UIColor* color = nil; CGImageRef inImage = image.CGImage; CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage]; if (cgctx == NULL) { return nil; /* error */ } size_t w = CGImageGetWidth(inImage); size_t h = CGImageGetHeight(inImage); CGRectrect = {{0,0},{w,h}}; CGContextDrawImage(cgctx, rect, inImage); unsigned char* data = CGBitmapContextGetData (cgctx); if (data != NULL) { int offset = 4*((w*round(point.y))+round(point.x)); int alpha = data[offset]; int red = data[offset+1]; int green = data[offset+2]; int blue = data[offset+3]; color = [UIColorcolorWithRed:(red/255.0f) green:(green/255.0f) blue: (blue/255.0f) alpha:(alpha/255.0f)]; } CGContextRelease(cgctx); if (data) { free(data); } return color; }

    5.字符串反转

    //第一种方法: - (NSString *)reverseWordsInString:(NSString *)str { NSMutableString *newString = [[NSMutableStringalloc] initWithCapacity:str.length]; for (NSInteger i = str.length - 1; i >= 0 ; i --) { unicharch = [strcharacterAtIndex:i]; [newStringappendFormat:@"%c", ch]; } return newString; } //第二种方法: - (NSString*)reverseWordsInString:(NSString*)str { NSMutableString *reverString = [NSMutableStringstringWithCapacity:str.length]; [strenumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRangesubstringRange, NSRangeenclosingRange, BOOL *stop) { [reverStringappendString:substring]; }]; return reverString; }

    7.禁止锁屏

    默认情况下,当设备一段时间没有触控动作时,iOS会锁住屏幕。但有一些应用是不需要锁屏的,比如视频播放器。

    [UIApplicationsharedApplication].idleTimerDisabled = YES; 或 [[UIApplicationsharedApplication] setIdleTimerDisabled:YES];

    8.获取汉字的拼音

    + (NSString *)transform:(NSString *)chinese { //将NSString装换成NSMutableString NSMutableString *pinyin = [chinese mutableCopy]; //将汉字转换为拼音(带音标) CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO); NSLog(@"%@", pinyin); //去掉拼音的音标 CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO); NSLog(@"%@", pinyin); //返回最近结果 return pinyin; }

    9.手动更改iOS状态栏的颜色

    - (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } }

    10.判断当前ViewController的跳转方式是push还是present

    NSArray *viewcontrollers=self.navigationController.viewControllers; if (viewcontrollers.count > 1) { if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) { //push方式 [self.navigationController popViewControllerAnimated:YES]; } } else { //present方式 [self dismissViewControllerAnimated:YEScompletion:nil]; }

    11.修改UITextField中Placeholder的文字颜色

    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

    12.根据色值生成图片

    - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRefcontext = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [colorCGColor]); CGContextFillRect(context, rect); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }

    转自:(http://www.jianshu.com/p/140de66f5e0d)

    转载请注明原文地址: https://ju.6miu.com/read-667402.html

    最新回复(0)