您的位置:首页 > 移动开发 > IOS开发

iOS 开发小技术点

2016-01-26 20:40 453 查看
判断scrollView滑动方向

先遵循UIScrollViewDelegate协议

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
static float newx = 0;
static float oldx = 0;
newx= self.contentView.scrollView.contentOffset.y ;
if (newx != oldx ) {
if (newx > oldx) {
[UIView animateWithDuration:.5 animations:^{
self.tabBerView.frame = CGRectMake(0, HXHeight, HXWidth, 49);
}];
}else if(newx < oldx){
[UIView animateWithDuration:.5 animations:^{
self.tabBerView.frame = CGRectMake(0, HXHeight-49, HXWidth, 49);

}];
}
oldx = newx;
}
}


手势添加步骤《轻拍手势为列》

1.遵循协议
UIGestureRecognizerDelegate
2.创建手势
self.swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

4.打开控件的用户交互
self.contentView.userInteractionEnabled = YES;
5.设置手势的相关设置
self.swipe.direction = UISwipeGestureRecognizerDirectionRight;//设置清扫方向(向右)
6.设置代理
self.swipe.delegate= self;
7.添加到控件上
[self.contentView addGestureRecognizer:self.swipe];

8.手势的方法实现
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
[self dismissViewControllerAnimated:YES completion:^{
}];
}


改变字体大小填满label

self.commentLabel.adjustsFontSizeToFitWidth = YES;


内容模式等比例缩小放大,多余的剪掉

self.contentImageView.clipsToBounds = YES;
self.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
等比例放大
self.contentImageView.contentMode = UIViewContentModeScaleAspectFill;
self.contentImageView.clipsToBounds = YES;


控件内容高度自适应

CGSize size = CGSizeMake(self.HXTitleLabel.frame.size.width, 0);
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:23.5]};//这里注意设置的字体要和其他地方设置该控件的字体大小保持一致
CGRect rect = [text.life_title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
CGRect frame = self.HXTitleLabel.frame;
frame.size.height = rect.size.height;
self.HXTitleLabel.frame = frame;


行间距

CGFloat heih = 20;

NSString * cLabelString = @"       这是测试UILabel行间距的text。这是测试UILabel行间距的text。\n       这是测试UILabel行间距的text。\n       这是测试UILabel行间距的text。这是测试UILabel行间距的text。这是测试UILabel行间距的text。这是测试UILabel行间距的text。";
UILabel * cLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, heih, 280, 200)];
cLabel.numberOfLines = 0;
cLabel.font = [UIFont fontWithName:fontName size:16];
cLabel.textColor = [UIColor grayColor];

NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:cLabelString];
NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setLineSpacing:8];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [cLabelString length])];
[cLabel setAttributedText:attributedString1];
[cLabel sizeToFit];
[self.view addSubview:cLabel];


在段落显示中显示不完时,设置省略号显示的位置

self.contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;


在View中做Controller的操作时可以通过响应者链来找

通过View找viewController
- (UINavigationController *)findViewController:(UIView *)sourceView
{
id target= sourceView;
while (target) {
target = ((UIResponder *)target).nextResponder;
if ([target isKindOfClass:[UINavigationController class]]) {
break;
}
}
return target;

}
用的地方
HXFriendDetController *det = [[HXFriendDetController alloc] init];
UINavigationController *viewController = [self findViewController:self];
[viewController pushViewController:det animated:YES];


图片呼吸效果

//呼吸效果定时器
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:7.f target:self selector:@selector(animationView:) userInfo:nil repeats:YES];
- (void)animationView:(NSTimer *)timer
{
CGAffineTransform transform = self.backgImageView.transform;
[UIView animateWithDuration:5.f animations: ^
{
self.backgImageView.transform = CGAffineTransformMakeScale(1.4f, 1.4f);
}];
[UIView animateWithDuration:5.f animations:^
{
self.backgImageView.transform = transform;
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: