你的激情被狗吃了吗

编程如逆水行舟,不进则退


  • 首页

  • 标签

  • 归档

  • 搜索

NSTimer+UIScrollView

发表于 2017-11-27 |
  • 在此备注一下RunLoop的知识

  • RunLoop的运行模式(CFRunLoopModeRef)

  • 一个 RunLoop包含若干个Mode,每个Mode又包含若干个Source/Timer/Observer,每次RunLoop启动时,只能指定其中一个Mode,这个Mode被称作 CurrentMode如果需要切换Mode,只能退出Loop,再重新指定一个Mode进入。

  • 系统默认模式

  • 1 NSDefaultRunLoopMode:App的默认Mode,通常主线程是在这个Mode下运行
  • 2 UITrackingRunLoopMode:界面跟踪Mode,用于ScrollView 追踪触摸滑动,保证界面滑动时不受其他 Mode 影响
  • 3 UIInitializationRunLoopMode:在刚启动App时第进入的第一个 Mode,启动完成后就不再使用
  • 4 GSEventReceiveRunLoopMode: 接受系统事件的内部 Mode,通常用不到
  • 5 NSRunLoopCommonModes:这是一个占位用的Mode,不是一种真正的Mode
1
2
3
4
5
6
// 新建NSTimer对象
self.testTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testTimerlog) userInfo:nil repeats:YES];

// 将NSTimer添加到RunLoop中,并且告诉系统,当前Tiemr只有在RunLoop的默认模式下才有效
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// 所以在UITrackingRunLoopMode模式下,定时器的方法不会执行,但定时器仍计时
  • 将NSTimer添加到RunLoop中,并且告诉系统,当前Tiemr只有在Tracking的默认模式下才有效
1
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
  • 将NSTimer添加到RunLoop中,并且告诉系统,在所有被”标记”common的模式都可以运行,UITrackingRunLoopMode和kCFRunLoopDefaultMode都被标记为了common模式,所以只需要将timer的模式设置为forMode:NSRunLoopCommonModes,就可以在默认模式和追踪模式都能够运行
1
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
  • 如果是通过scheduledTimerWithTimeInterval创建的NSTimer, 默认就会添加到RunLoop得DefaultMode中 , 所以它会自动运行
1
2
3
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(show) userInfo:nil repeats:YES];
// 虽然默认已经添加到DefaultMode中,但是我们也可以自己修改它的模式
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

iOS阻止子视图相应父视图事件

发表于 2017-11-27 |

实现如下代理方法

1
2
3
4
5
6
7
8
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:你想屏蔽点击事件的View]) {
return NO;
}
return YES;
}

iOS选择照片九宫格动态布局

发表于 2017-11-27 |

直接上源码

阅读全文 »

iOS的桌面背景运动效果

发表于 2017-11-27 |
1
2
3
4
5
6
7
8
9
10
11
12
13
_backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(-50, -50, Screen_width+100, Screen_Height+100)];
_backgroundImage.image = [UIImage imageNamed:@"bg_1"];
[self.view addSubview:_backgroundImage];

UIInterpolatingMotionEffect * backEffX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];// type表示沿水平方向运行效果
backEffX.maximumRelativeValue = @(-50);
backEffX.minimumRelativeValue = @(50);
[self.backgroundImage addMotionEffect:backEffX];

UIInterpolatingMotionEffect * backEffY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
backEffY.maximumRelativeValue = @(-50);
backEffY.minimumRelativeValue = @(50);
[self.backgroundImage addMotionEffect:backEffY];

iOS根据身份证号获取年龄生日性别

发表于 2017-11-27 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//根据身份证号获取生日
-(NSString *)birthdayStrFromIdentityCard:(NSString *)numberStr
{
NSMutableString *result = [NSMutableString stringWithCapacity:0];
NSString *year = nil;
NSString *month = nil;

BOOL isAllNumber = YES;
NSString *day = nil;
if([numberStr length]<14)
return result;

//**截取前14位
NSString *fontNumer = [numberStr substringWithRange:NSMakeRange(0, 13)];

//**检测前14位否全都是数字;
const char *str = [fontNumer UTF8String];
const char *p = str;
while (*p!='\0') {
if(!(*p>='0'&&*p<='9'))
isAllNumber = NO;
p++;
}
if(!isAllNumber)
return result;

year = [numberStr substringWithRange:NSMakeRange(6, 4)];
month = [numberStr substringWithRange:NSMakeRange(10, 2)];
day = [numberStr substringWithRange:NSMakeRange(12,2)];

[result appendString:year];
[result appendString:@"-"];
[result appendString:month];
[result appendString:@"-"];
[result appendString:day];
return result;
}
阅读全文 »

UILabel在设置了行间距和字间距后计算高度

发表于 2017-11-27 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (CGFloat)textHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)textSize {

//设置行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3];

//把行间距和字间距加入高度计算的方法
NSDictionary*attrs =@{NSFontAttributeName: [UIFont systemFontOfSize:textSize],NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@1.5f};

CGRect rect = [text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine attributes:attrs context:nil];

//返回计算出的行高
return ceilf(rect.size.height);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
UILabel *msgLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-30, 100)];
msgLab.backgroundColor = [UIColor clearColor];
msgLab.textColor = [[UIColor whiteColor] colorWithAlphaComponent:.8];
msgLab.numberOfLines = 0;
msgLab.lineBreakMode = NSLineBreakByWordWrapping;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[bannerDic objectForKey:@"msgText"]];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3];//调整行间距
//这个设置在字体无法显示时一省略号表示
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];

[attributedString addAttribute:NSFontAttributeName value:msgFont range:NSMakeRange(0, attributedString.length)];
msgLab.attributedText = attributedString;
[self addSubview:msgLab];

判断图片格式

发表于 2017-11-27 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
+(NSString *)typeForImageData:(NSData *)data
{
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"jpeg";

case 0x89:
return @"png";

case 0x47:
return @"gif";

case 0x49:

case 0x4D:
return @"tiff";
}
return nil;
}

iOS分角设置View圆角

发表于 2017-11-27 |
1
2
3
4
5
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, Screen_width-30, 258) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(2, 2)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = CGRectMake(0, 0, Screen_width-30, 258);
maskLayer.path = maskPath.CGPath;
self.picTure.layer.mask = maskLayer;
  • 注意:当分角设置View的圆角的时候要确定的知道View的绝对Frame不然会出现偏差。
12
LiuXin

LiuXin

编程如逆水行舟,不进则退

18 日志
20 标签
© 2018 LiuXin
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.3