【问题标题】:Only Visible View Animation仅可见视图动画
【发布时间】:2015-06-29 19:45:56
【问题描述】:

我有以下实现。每个视图都有动画,但我的问题是所有动画都运行,无论可见或不可见。我只希望当前看到(活动)的视图动画作品,其他视图动画被禁用,除非用户滚动它。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{

    if (view == nil){
        if(index==0)
        {
            view=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 600, 600)];
            ((UIImageView *)view).animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"Walking-1.png"],
                                 [UIImage imageNamed:@"Walking-2.png"],
                                 [UIImage imageNamed:@"Walking-3.png"],
                                 [UIImage imageNamed:@"Walking-4.png"],
                                 [UIImage imageNamed:@"Walking-5.png"],nil];

            ((UIImageView *)view).animationDuration = 1.5;
            [((UIImageView *)view) startAnimating];
        }

        else if(index==1)
        {
            view=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 600, 600)];
            ((UIImageView *)view).animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"Biking-1.png"],
                              [UIImage imageNamed:@"Biking-2.png"],
                              [UIImage imageNamed:@"Biking-3.png"],
                              [UIImage imageNamed:@"Biking-4.png"],                                                   
                              [UIImage imageNamed:@"Biking-5.png"],nil];

            ((UIImageView *)view).animationDuration = 1.5;
            [((UIImageView *)view) startAnimating];
        }

        view.contentMode = UIViewContentModeCenter;
        [view.layer setMasksToBounds:YES];
    }
    return view;
}

【问题讨论】:

    标签: ios icarousel


    【解决方案1】:

    iCarouselDelegate 有一个方法carouselCurrentItemIndexDidChange 你应该在那里停止/开始你的动画,但不是在数据源方法中。因此,将所有动画保存到索引等于项目索引的数组中,并使用carouselCurrentItemIndexDidChange 中轮播的currentItemIndex 属性来启动适当的动画。您需要将之前的索引存储在类中的某个位置,以便能够在开始新动画之前停止之前的动画。

        @import UIKit;
    
        @interface CarouselController: UIViewController
        @property(nonatomic, readonly) NSArray *carouselItems;
        @property(nonatomic, assign) NSInteger lastItemIndex;
        @end
    
        @implementation CarouselController
    
        - (instancetype)init {
        self = [super init];
    
        if (self) {
            [self initializeCarouselContent];
        }
    
        return self;
    }
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
    
        if (self) {
            [self initializeCarouselContent];
        }
    
        return self;
    }
    
    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
        if (self) {
            [self initializeCarouselContent];
        }
    
        return self;
    }
    
    - (void)initializeCarouselContent {
        self.lastItemIndex = 0;
        _carouselItems = @[[CarouselController activityAnimationView: @"Walking"], [CarouselController activityAnimationView: @"Biking"]];
    
        UIImageView *currentAnimation = self.carouselItems[self.lastItemIndex];
        [currentAnimation startAnimating];
    }
    
        + (UIImageView *)activityAnimationView:(NSString *)activity {
            UIImageView *result = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 600, 600)];
    
            NSMutableArray *activityImages = [[NSMutableArray alloc] init];
            for (int activityIndex = 1; activityIndex <= 5; activityIndex ++) {
                NSString *imageName = [NSString stringWithFormat:@"%@-%i.png", activity, activityIndex];
                [activityImages addObject:[UIImage imageNamed:imageName]];
            }
            result.animationImages = activityImages;
            result.animationDuration = 1.5;
    
            result.contentMode = UIViewContentModeCenter;
            [result.layer setMasksToBounds:YES];
    
            return result;
        }
    
    
       - (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel { 
            return [_carouselItems count];
         }
    
        - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *) view{
    
            if (view == nil){
                view = self.carouselItems[index];
            }
    
            return view;
        }
    
        - (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel {
            if (carousel.currentItemIndex != self.lastItemIndex) {
                UIImageView *currentAnimation = self.carouselItems[self.lastItemIndex];
                [currentAnimation stopAnimating];
    
                self.lastItemIndex = carousel.currentItemIndex;
    
                currentAnimation = self.carouselItems[self.lastItemIndex];
                [currentAnimation startAnimating];
            }
        }
    
        @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 2016-04-19
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2013-06-06
      相关资源
      最近更新 更多