将 UIView 动画 Swift 代码翻译成 Objective-C

Translate UIView animation Swift code to Objective-C

我需要将此 Swift 代码翻译成 Objective-C。

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

如果无法转换为Objective-C,请告诉我那里发生了什么,我会转换它。

P.S。 rotatedViewUIView 的 class 并且 rotatedViews 是一个空数组是一种 rotatedView class.

根据@Haris Nadeem 的建议,我正在分享我到目前为止所做的事情。

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];
}
}

我猜你不会给出这条线:var rotatedViews: [RotatedView]()

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

让我们打破链接,并命名一些中间变量,因为在一行中这样做很酷,但有时更难调试,而且在 Objective-C 中,它会非常混乱 =>

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

那么那里发生了什么:
compacted:只保留 class RotatedView.
的子视图 sorted:我们根据 tag 属性对这些视图进行排序。
在最后一个,你完全误解了正在发生的事情。 我们必须 rotatedViews sorted 之前的所有视图,如果他们有 backView 我们也添加它。

未测试(没有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)
    }
}