Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 仅可见视图动画_Ios_Icarousel - Fatal编程技术网

Ios 仅可见视图动画

Ios 仅可见视图动画,ios,icarousel,Ios,Icarousel,我有以下实现。每个视图都有动画,但我的问题是所有动画都会运行,无论是可见还是不可见。我只希望当前看到的(活动)视图动画工作,其他视图动画将被禁用,除非用户滚动它 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{ if (view == nil){ if(index==0) {

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

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

iCarouselDelegate
有一个方法
carouseCurrentitemindexdidchange
您应该在那里停止/启动动画,但不能在数据源方法中。因此,将所有动画保存到索引等于项目索引的数组中,并在
carouselCurrentItemIndexDidChange
中使用carousel的
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
@import-UIKit;
@接口转盘控制器:UIViewController
@属性(非原子,只读)NSArray*旋转木马;
@属性(非原子,赋值)NSInteger lastItemIndex;
@结束
@实现转盘控制器
-(instancetype)初始化{
self=[super init];
如果(自我){
[自初始化CAURESELCONTENT];
}
回归自我;
}
-(instancetype)initWithCoder:(NSCoder*)aDecoder{
self=[super initWithCoder:aDecoder];
如果(自我){
[自初始化CAURESELCONTENT];
}
回归自我;
}
-(instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
[自初始化CAURESELCONTENT];
}
回归自我;
}
-(无效)初始化CAURELCONTENT{
self.lastItemIndex=0;
_carouselItems=@[[CarouselController活动动画视图:@“行走”],[CarouselController活动动画视图:@“骑自行车”];
UIImageView*currentAnimation=self.carouselItems[self.lastItemIndex];
[currentAnimation startAnimating];
}
+(UIImageView*)活动动画视图:(NSString*)活动{
UIImageView*result=[[UIImageView alloc]initWithFrame:CGRectMake(30,30,600,600)];
NSMutableArray*activityImages=[[NSMutableArray alloc]init];
对于(int activityIndex=1;activityIndex