Button 使用objc_setAssociatedObject关联UIimageView

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

设置关联的key

#import 
char* const buttonKey = "buttonKey";


Button 使用objc_setAssociatedObject关联UIimageView_第1张图片
二级列表展开时的小三角形.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_setAssociatedObject关联UIimageView)