iOS 开发小技术点

滑动方向判断与手势操作优化

判断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;
     }];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值