iOS transitionFromView 翻轉(zhuǎn)View

dsdsdsdezgif.com-resize.gif

用了UIView分類UIViewAnimationWithBlocks里的類方法:

+ (void)transitionFromView:(UIView *)fromView //第一個(gè)view
                                   toView:(UIView *)toView //第二個(gè)view
                                 duration:(NSTimeInterval)duration //翻轉(zhuǎn)時(shí)間
                                   options:(UIViewAnimationOptions)options//翻轉(zhuǎn)方向
                             completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview

需要注意的是fromView和toView需要添加到同一個(gè)父view上,這樣才會(huì)讓父view翻轉(zhuǎn),不然你整個(gè)window都在翻轉(zhuǎn)別怪我??

- (UIView *)firstView
{
    if (!_firstView)
    {
        _firstView = [UIView new];
        _firstView.frame = self.bounds;
        
        UIImageView *imgView = [UIImageView new];
        imgView.frame = _firstView.bounds;
        imgView.clipsToBounds = YES;
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.image = [UIImage imageNamed:@"car1"];
        [_firstView addSubview:imgView];
        
        UILabel *label = [UILabel new];
        label.frame = CGRectMake(0, self.frame.size.height - 50, self.frame.size.width, 50);
        label.backgroundColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.text = @"我是正面";
        [_firstView addSubview:label];
        [self addSubview:_firstView];
        _firstView.layer.cornerRadius = 5;
        _firstView.clipsToBounds = YES;
    }
    return _firstView;
}

- (SecondView *)secondView
{
    if (!_secondView)
    {
        _secondView = [[SecondView alloc] initWithFrame:self.bounds];
        _secondView.layer.cornerRadius = 5;
        _secondView.clipsToBounds = YES;
    }
    return _secondView;
}

第一個(gè)view加到父view上,第二個(gè)只初始化,不添加到父view

- (void)setTitle:(NSString *)title
{
    _title = title;
    
    if ([title isEqualToString:@"反面"])
    {
        if (self.click)
        {
            self.click(@"正面");
        }
        [UIView transitionFromView:self.firstView toView:self.secondView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
            NSLog(@"翻轉(zhuǎn)到了正面");
        }];
    }else
    {
        if (self.click)
        {
            self.click(@"反面");
        }
        [UIView transitionFromView:self.secondView toView:self.firstView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
            NSLog(@"翻轉(zhuǎn)到了反面");
        }];
    }
}

FlipAnimationView

以上。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容