Search code examples
iosobjective-cswiftios-animations

Translate UIView animation Swift code to Objective-C


I need to translate this Swift code to Objective-C.

animationView?.subviews
     .compactMap({ $0 as? RotatedView })
     .sorted(by: { $0.tag < $1.tag })
     .forEach { itemView in
         rotatedViews.append(itemView)
         if let backView = itemView.backView {
             rotatedViews.append(backView)
         }
     }

If you cannot convert to Objective-C, please tell me what's happening there, I will convert it.

P.S. rotatedView is class of UIView and rotatedViews is an empty array is kind of rotatedView class.

As suggest from @Haris Nadeem i am sharing what i have done till now.

NSMutableArray *dummy = [[NSMutableArray alloc]init];
for(id view in self.animationView.subviews){
    if([view isKindOfClass:[RotatedView class]])
        [dummy addObject:(RotatedView *)view];
}
for(RotatedView *preView in dummy ){
//        if( view.tag>preView.tag){
//            [dummy addObject:view];
//            preView.tag=view.tag;
}
for( RotatedView *iteamView in [rotatedViews addObject:iteamView]){
     if (preView.backView ==  iteamView.backView)
         [rotatedViews addObject:preView.backView];
}
}

Solution

  • I guess you are not giving this line: var rotatedViews: [RotatedView]()

    animationView?.subviews
       .compactMap({ $0 as? RotatedView })
       .sorted(by: { $0.tag < $1.tag })
       .forEach { itemView in
           rotatedViews.append(itemView)
           if let backView = itemView.backView {
               rotatedViews.append(backView)
           }
       }
    

    Let's break the chaining, and name some intermediaries variables because doing it in one line is cool, but harder sometimes to debug, and in Objective-C, it will be quite messy =>

    let compacted = animationView?.subviews({ $0 as? RotatedView })
    let sorted = compacted.sorted(by: { $0.tag < $1.tag })
    sorted.forEach { itemView in
        rotatedViews.append(itemView)
        if let backView = itemView.backView {
            rotatedViews.append(backView)
        }
    }
    

    So what's happening there:
    compacted: Keep only the subviews that are of class RotatedView.
    sorted: We sort theses view according to their tag properties.
    On the last one, you misunderstood completely what is happening. We had to rotatedViews all the previous views of sorted, and if they have a backView we add it too.

    Not tested (written without debugger/compiler/XCode):

    NSMutableArray *rotatedViews = [[NSMutableArray alloc] init]; //Final array
    
    NSMutableArray *compacted = [[NSMutableArray alloc] init];
    for (UIView *aView in animationView.subviews)
    {
        if ([aView isKindOfClass:[RotatedView class]])
        {
            [compacted addObject:aView];
        }
    }
    
    NSArray *sorted = [compacted sortedArrayUsingComparator:^NSComparisonResult(RotatedView *view1, RotatedView *view2){
        NSInteger tag1 = [view1 tag];
        NSInteger tag2 = [view2 tag];
        return [@(tag1) compare:@(tag2)];
    }];
    
    for (RotatedView *itemView in sorted) //That's equivalent to forEach( itemView in
    {
        [rotatedViews addObject:itemView]; //That's equivalent to rotatedViews.append(itemView)
        UIView *backView = [itemView backView]; //That's equivalent to if let backView = itemView.backView
        if (backView)  //That's equivalent to if let backView = itemView.backView
        {
            [rotatedViews addObject:backView]; //That's equivalent to rotatedViews.append(backView)
        }
    }