方法
Extension methods:
//取消单击事件(上次执行的请求)
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singTap) object:nil];
//延迟调用
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
[self performSelector:@selector(singTap) withObject:nil afterDelay:.5];
//transform
CGAffineTransformMakeScale 在原来的基础上缩放
CGAffineTransformeScale 在自身的基础上缩放
//重写setFrame方法
//重写setFrame方法 -(void)setFrame:(CGRect)frame{ /* 1、取出初始化的时候answerView的frame 2、用传过来的frame中的origin来代替初始化的frame 3、把最终的frame给answerView */ CGRect frame1 = self.frame; frame1.origin = frame.origin; //不能直接写成 self.frame = frame,因为在setFrame方法中不能调用setFrame方法,父类的setFrame方法是直接赋值 [super setFrame:frame1]; }
//创建一个NSTimer定时器,需要手动销毁
// // ViewController.m // 03-UIImageViewDemo // // Created by qingyun on 16/3/24. // Copyright © 2016年 河南青云信息技术有限公司. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) NSTimer *timer; @end @implementation ViewController -(void)changeImageHighlightedState{ UIImageView *imageView = [self.view viewWithTag:101]; imageView.highlighted = !imageView.highlighted; } - (void)viewDidLoad { [super viewDidLoad]; #if 0 UIImage *image = [UIImage imageNamed:@"dog1"]; UIImage *highlightedImage = [UIImage imageNamed:@"dog2"]; //创建并添加imageView UIImageView *imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage]; [self.view addSubview:imageView]; //frame imageView.frame = CGRectMake(100, 100, 100, 100); imageView.tag = 101; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImageHighlightedState) userInfo:nil repeats:YES]; #else //添加火焰视图 UIImageView *fireView = [[UIImageView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:fireView]; //创建一个可变数组,存储图片 NSMutableArray *images = [NSMutableArray array]; for (int i = 0; i < 17; i++) { //注意,图片格式非png的话,需要指定后缀名 NSString *imageName = [NSString stringWithFormat:@"campFire%02d.gif",i + 1]; UIImage *image = [UIImage imageNamed:imageName]; [images addObject:image]; } //设置帧动画数组 fireView.animationImages = images; //设置动画时间 fireView.animationDuration = 1; //重复次数(0代表无限) fireView.animationRepeatCount = 0; //启动动画 [fireView startAnimating]; #endif _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(flySnow) userInfo:nil repeats:YES]; // Do any additional setup after loading the view, typically from a nib. } -(void)flySnow{ //创建雪花 UIImage *snow = [UIImage imageNamed:@"flake"]; UIImageView *snowView = [[UIImageView alloc] initWithImage:snow]; [self.view addSubview:snowView]; //设置起始位置 int screenW = [UIScreen mainScreen].bounds.size.width; //雪花的起始X int x1 = arc4random() % screenW; //雪花的随机倍数 double scale = 1.0 + (arc4random() % 100) / 100.0; snowView.frame = CGRectMake(x1, -(30 * scale), 30 * scale, 30 * scale); //动画时间随机倍数 double durationScale = 1.0 + (arc4random() % 100) / 100.0; //设置结束位置 [UIView animateWithDuration:5 * durationScale animations:^{ //雪花最终的随机倍数 double scale2 = 1.0 + (arc4random() % 100) / 100.0; int x2 = arc4random() % screenW; int screenH = [UIScreen mainScreen].bounds.size.height; snowView.frame = CGRectMake(x2, screenH - 20 *scale2, 20 * scale2, 20 * scale2); } completion:^(BOOL finished) { [snowView removeFromSuperview]; }]; } -(void)dealloc{ [_timer invalidate]; _timer = nil; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
- (void)dealloc{
[_timer invalidate];
_timer = nil;
}
//如果输入的不是数字 判断字符串内容是否是有效数字
(例)
一丶 if ((![_cometextField.text integerValue] || ![_outTextField.text integerValue]))
二丶obj.keyboardType = UIKeyboardTypeDecimalPad; (设置键盘的类型)
三丶通过 UITextFieldDelegate 的 shouldChangeCharactersInRange: 方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { //return [self validateNumberByASCII:string]; //return [self validateNumberByRange:string]; return [self validateNumberByRegExp:string]; //推荐方式 }
四丶正则表达式 (代码中方法为自定义方法)
/** * 『正则表达式;推荐使用,不用循环遍历,控制更灵活』判断字符串内容是否是有效数字 * * @param string 需要验证的字符串 * * @return 字符串内容是否是有效数字 */ - (BOOL)validateNumberByRegExp:(NSString *)string { BOOL isValid = YES; NSUInteger len = string.length; if (len > 0) { NSString *numberRegex = @"^[0-9]*$"; NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex]; isValid = [numberPredicate evaluateWithObject:string]; } return isValid; }
五丶字符范围 (代码中方法为自定义方法)
/** * 『字符范围』判断字符串内容是否是有效数字 * * @param string 需要验证的字符串 * * @return 字符串内容是否是有效数字 */ - (BOOL)validateNumberByRange:(NSString *)string { BOOL isValid = YES; NSUInteger len = string.length; if (len > 0) { NSCharacterSet *validNumberCS = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; NSUInteger singleStrIndex = 0; do { NSString *singleStr = [string substringWithRange:NSMakeRange(singleStrIndex, 1)]; NSRange range = [singleStr rangeOfCharacterFromSet:validNumberCS]; if (range.length == 0) { isValid = NO; break; } singleStrIndex++; } while (singleStrIndex < len); } return isValid; }
六丶ASCII码
/** * 『ASCII码』判断字符串内容是否是有效数字 * * @param string 需要验证的字符串 * * @return 字符串内容是否是有效数字 */ - (BOOL)validateNumberByASCII:(NSString *)string { BOOL isValid = YES; NSUInteger len = string.length; if (len > 0) { for (NSUInteger i=0; i<len; i++) { NSUInteger asciiCode = [string characterAtIndex:i]; if (asciiCode < 48 || asciiCode > 57) { isValid = NO; break; } } } return isValid; }
//图片渲染
UIImage *image = [[UIImage imageNamed:@"stepper"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];