美文网首页iOS Developer
Button 使用objc_setAssociatedObjec

Button 使用objc_setAssociatedObjec

作者: Zhui_Do | 来源:发表于2017-03-27 15:22 被阅读57次

    利用Runtime不仅可以使用objc_setAssociatedObject方法来为类别中添加属性 也可以这样用这个关联方法获取到与之关联的object对象 在一些场景中使用这样的方法还是很便捷的!

    设置关联的key
    
    #import <objc/runtime.h>
    char* const buttonKey = "buttonKey";
    
    
    
    二级列表展开时的小三角形.png
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setFrame:sectionView.bounds];
    
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    
    [sectionView addSubview:button];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];
    
    [line setImage:[UIImage imageNamed:@"line_real"]];
    
    [sectionView addSubview:line];
    
    if (groupModel.isOpened) {
    
    UIImageView * _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-16)/2, 14, 16)];
    
    [_imgView setImage:[UIImage imageNamed:@"ico_list"]];
    
    [sectionView addSubview:_imgView];
    
    CGAffineTransform currentTransform = _imgView.transform;
    
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, M_PI/2); // 在现在的基础上旋转指定角度
    
    _imgView.transform = newTransform;
    
    objc_setAssociatedObject(button, buttonKey, _imgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    }else{
    
    UIImageView * _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-16)/2, 14, 16)];
    
    [_imgView setImage:[UIImage imageNamed:@"ico_list"]];
    
    [sectionView addSubview:_imgView];
    
    objc_setAssociatedObject(button, buttonKey, _imgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    }
    
    

    通过objc_getAssociatedObject获取与button关联的image view

    - (void)buttonPress:(UIButton *)sender//headButton点击
    {
        GroupModel *groupModel = dataSource[sender.tag];
        UIImageView *imageView =  objc_getAssociatedObject(sender,buttonKey);
    
        
        if (groupModel.isOpened) {
                [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
                    CGAffineTransform currentTransform = imageView.transform;
                    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, -M_PI/2); // 在现在的基础上旋转指定角度
                    imageView.transform = newTransform;
    
    
                } completion:^(BOOL finished) {
                    
    
                }];
            
            
            
        }else{
            
                [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveLinear animations:^{
    
                    CGAffineTransform currentTransform = imageView.transform;
                    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, M_PI/2); // 在现在的基础上旋转指定角度
                    imageView.transform = newTransform;
                
                } completion:^(BOOL finished) {
                    
                }];
            }
    
        groupModel.isOpened = !groupModel.isOpened;
    
        [expandTable reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    

    相关文章

      网友评论

        本文标题:Button 使用objc_setAssociatedObjec

        本文链接:https://www.haomeiwen.com/subject/ifsrottx.html