美文网首页
AsyncDisplayKit2.0中新功能 Automatic

AsyncDisplayKit2.0中新功能 Automatic

作者: tljackyi | 来源:发表于2016-12-12 22:55 被阅读287次

    Automatic Subnode Management(ASM)是AsyncDisplayKit2.0中的新功能,当使用 Layout Transition API进行布局时可以,可以省略掉addSubnode:removeFromSupernode,node是否添加或移除由layoutSpecThatFits:中的布局决定, 当需要调整布局时可以调用setNeedsLayout再次调用layoutSpecThatFits:

    AsyncDisplayKit2.0以前的方式

    - (instancetype)initWithModelCollectProductLis:(TTCollectProductModel *)product
    {
        if (!(self = [super init])) {
            return nil;
        }
    
        self.backgroundColor = [UIColor whiteColor];
        self.imageViewNode = [[ASNetworkImageNode alloc] init];
        self.imageViewNode.URL = [GlobalFunc urlWithStr:product.ImgUrl];
        self.imageViewNode.defaultImage = [UIImage imageNamed:@"defaultImg"];
        self.imageViewNode.placeholderFadeDuration = 0.3;
        self.imageViewNode.layerBacked = YES;
        
        self.priceTextNode = [[ASTextNode alloc] init];
        self.priceTextNode.maximumNumberOfLines = 1;
        self.priceTextNode.layerBacked = YES;
        
        NSString *price = [NSString stringWithFormat:@"¥%@",[GlobalFunc checkPrice:product.ProductVipPrice.doubleValue]];
        self.priceTextNode.attributedText = [NSAttributedString attributedStringPriceText:price];
        
        self.oldPriceTextNode = [[ASTextNode alloc] init];
        self.oldPriceTextNode.maximumNumberOfLines = 1;
        self.oldPriceTextNode.layerBacked = YES;
        NSString *oldPrice = [NSString stringWithFormat:@"¥%.0f",product.ProductSalePrice.doubleValue];
        self.oldPriceTextNode.attributedText = [NSAttributedString attributedStringOldPriceText:oldPrice];
     
        self.contentTextNode = [[ASTextNode alloc] init];
        self.contentTextNode.maximumNumberOfLines = 2;
        self.contentTextNode.truncationMode = NSLineBreakByTruncatingTail;
        self.contentTextNode.layerBacked = YES;
        self.contentTextNode.attributedText = [NSAttributedString attributedStringProductNameText:product.Productname];
     
        [self.view addSubnode:self.imageViewNode];
        [self.view addSubnode:self.priceTextNode];
        [self.view addSubnode:self.oldPriceTextNode];
        [self.view addSubnode:self.contentTextNode];
        
        return self;
    }
    
    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
    {
        ASRatioLayoutSpec *imageRatioSpec = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1 child:self.imageViewNode];
        
        ASLayoutSpec *spacer = [[ASLayoutSpec alloc] init];
        spacer.flexGrow = YES;
        
        ASStackLayoutSpec *priceStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[self.priceTextNode, spacer, self.oldPriceTextNode]];
        
        ASStackLayoutSpec *contentStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[priceStackSpec, self.contentTextNode]];
        
        ASInsetLayoutSpec *contentinsetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 10, 10, 10) child:contentStackSpec];
        
        ASStackLayoutSpec *cellStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[imageRatioSpec, contentinsetSpec]];
        return cellStackSpec;
    }
    
    

    AsyncDisplayKit2.0方式

    - (instancetype)initWithModelCollectProductLis:(TTCollectProductModel *)product
    {
        if (!(self = [super init])) {
            return nil;
        }
        self.automaticallyManagesSubnodes = YES;
    
        self.backgroundColor = [UIColor whiteColor];
        self.imageViewNode = [[ASNetworkImageNode alloc] init];
        self.imageViewNode.URL = [GlobalFunc urlWithStr:product.ImgUrl];
        self.imageViewNode.defaultImage = [UIImage imageNamed:@"defaultImg"];
        self.imageViewNode.placeholderFadeDuration = 0.3;
        self.imageViewNode.layerBacked = YES;
        
        self.priceTextNode = [[ASTextNode alloc] init];
        self.priceTextNode.maximumNumberOfLines = 1;
        self.priceTextNode.layerBacked = YES;
        
        NSString *price = [NSString stringWithFormat:@"¥%@",[GlobalFunc checkPrice:product.ProductVipPrice.doubleValue]];
        self.priceTextNode.attributedText = [NSAttributedString attributedStringPriceText:price];
        
        self.oldPriceTextNode = [[ASTextNode alloc] init];
        self.oldPriceTextNode.maximumNumberOfLines = 1;
        self.oldPriceTextNode.layerBacked = YES;
        NSString *oldPrice = [NSString stringWithFormat:@"¥%.0f",product.ProductSalePrice.doubleValue];
        self.oldPriceTextNode.attributedText = [NSAttributedString attributedStringOldPriceText:oldPrice];
        
        self.contentTextNode = [[ASTextNode alloc] init];
        self.contentTextNode.maximumNumberOfLines = 2;
        self.contentTextNode.truncationMode = NSLineBreakByTruncatingTail;
        self.contentTextNode.layerBacked = YES;
        self.contentTextNode.attributedText = [NSAttributedString attributedStringProductNameText:product.Productname];
           
        return self;
    }
    
    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
    {
        ASRatioLayoutSpec *imageRatioSpec = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1 child:self.imageViewNode];
        
        ASLayoutSpec *spacer = [[ASLayoutSpec alloc] init];
        spacer.style.flexGrow = YES;
        
        ASStackLayoutSpec *priceStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[self.priceTextNode, spacer, self.oldPriceTextNode]];
        
        ASStackLayoutSpec *contentStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[priceStackSpec, self.contentTextNode]];
        
        ASInsetLayoutSpec *contentinsetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 10, 10, 10) child:contentStackSpec];
        
        ASStackLayoutSpec *cellStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[imageRatioSpec, contentinsetSpec]];
        return cellStackSpec;
    }
    

    相关文章

      网友评论

          本文标题:AsyncDisplayKit2.0中新功能 Automatic

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