【问题标题】:transitionWithView gets called repeatedly after ViewController is dismissed在 ViewController 被解除后,transitionWithView 被重复调用
【发布时间】:2012-10-24 23:35:56
【问题描述】:

这是在 ModalViewController 中使用 StoryBoardARC

当我关闭视图时,我看到这条线...... NSLog(@"%d %@",[imgArray count],fileName); ... 被一遍又一遍地打印出来。

当视图被关闭时,我如何杀死函数transitionWithView/animateWithDuration

遗漏了一些非常重要的东西!

.h

#import <UIKit/UIKit.h>

@interface InfoVC : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageview;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

@end

.m

#import "InfoVC.h"
#import <QuartzCore/QuartzCore.h>

@interface InfoVC ()

@end

@implementation InfoVC {
    int randomInt;
    NSArray *imgs;
    NSMutableArray *imgArray;
    NSTimer *myTimer;
    NSTimer *myTimer2;
    NSString *currentImg;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    imgs = [NSArray arrayWithObjects:
                      @"01.jpg",
                      @"02.jpg",
                      @"03.jpg",
                      nil];
    imgArray = [imgs mutableCopy];
    [self initialImage];
}


- (void)initialImage
{
    _imageview.contentMode = UIViewContentModeLeft;

    int rnd = [imgArray count];
    randomInt = (arc4random()%rnd);
    currentImg = [imgArray objectAtIndex:randomInt];    
    UIImage *image = [UIImage imageNamed:currentImg];

    [_imageview setImage:image];
    _imageview.bounds = CGRectMake(0, 0, image.size.width, image.size.height);

    _scrollview.contentSize = image.size;
    _scrollview.contentOffset = CGPointMake(-150.0, 0.0);

    [imgArray removeObjectAtIndex: randomInt];

    [self animate];
}

- (void)randomImage
{        
    if ([imgArray count]==0) {
        imgArray = [imgs mutableCopy];
    }

    int rnd = [imgArray count];
    randomInt = (arc4random()%rnd);
    NSString *fileName = [imgArray objectAtIndex:randomInt];

    NSLog(@"%d %@",[imgArray count],fileName);

    [imgArray removeObjectAtIndex: randomInt];

    UIImage * toImage = [UIImage imageNamed:fileName];


    void (^completion)(void) = ^{
        [self animate];
    };

    [UIView transitionWithView:self.view
                      duration:1.75f
                       options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
                    animations:^
                    {
                        _imageview.image = toImage;
                    }
                    completion:^(BOOL finished)
                    {
                        completion();
                    }
    ];

}

- (void)animate{

    CGPoint offset = _scrollview.contentOffset;

    float flt = 150.0;

    if (_scrollview.contentOffset.x == flt)
    {
        offset.x = 0.0 ;
    }
    else
    {
        offset.x = flt ;
    }

    void (^completion2)(void) = ^{
        [self randomImage];
    };

    [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseIn
                     animations:^{
                         _scrollview.contentOffset = offset; 
                     }completion:^(BOOL completed){
                         completion2();
                     }
     ];


}

- (void)viewDidUnload
{
    [super viewDidUnload];

    [_scrollview.layer removeAllAnimations];
    [_imageview.layer removeAllAnimations];
}


- (IBAction)dismissInfo:(id)sender
{
    [_scrollview.layer removeAllAnimations];
    [_imageview.layer removeAllAnimations];

    [self dismissModalViewControllerAnimated:YES];
}

【问题讨论】:

  • 你通过调用另一个和另一个调用相同的调用函数来生成无限循环.....我的建议你需要更改模型
  • 是的,我想要一个无限循环,它只是不停地跳动。自从发布以来,我已经嵌套了两个块,但问题是,一旦控制器被移除,randomImage 块就会被快速连续调用 - 比控制器存在时快得多

标签: objective-c uiview objective-c-blocks quartz-graphics


【解决方案1】:

我尝试使用transitionWithViewCABasicAnimation,使用NSTimer 等大量复古的东西。

最后我做到了:

[_imageview removeFromSuperview];

当关闭视图时,一切都很好。

但是从这里的其他帖子看来,在将 alpha 转换应用于 UIScrollview 时出现了一个错误 - 对视图的引用在透明时会丢失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多