在动画UIView动画时,x位置不会改变?

时间:2013-01-22 11:51:44

标签: ios uianimation

我有以下代码行,其中在使用动画移动视图时,我通过打印日志检查了移动元素的x位置。每次移动视图时都显示相同的x位置。

-(void)removeObject
{
    for (UIView *subview in [self subviews])
    {
        if (subview.frame.origin.x+subview.frame.size.width==0) {
            [subview removeFromSuperview];
        }
    }
}


-(void)animateView:(UIView*)view
{
[UIView animateWithDuration:5.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionRepeat
                 animations:^{
                     [view setFrame:CGRectMake(-100, 0, 200, 100)];
                 } completion:^(BOOL finished){
                 }];
}


-(void)addElement
{
    NSLog(@"add element called ");

    NSArray *subviews=[self subviews];
    UIView *subView=[self.datasource viewForIndex:count inMarque:self];

    if (![subviews containsObject:(id)subView]) {
        [subView setFrame:CGRectMake(320, 0, 100, 100)];
        [subView setBackgroundColor:[UIColor blueColor]];

        NSLog(@"subview frame is %f",subView.frame.origin.x);

        if (subView.frame.origin.x+subView.frame.size.width<300 || [subviews count]==0)
        {
        NSLog(@"add subview called");
        [self addSubview:subView];
        count=(count+1==numberOfElement)?0:count+1;
        }

        [self removeObject];
        [self animateView:subView];
    }
}


- (void)drawRect:(CGRect)rect
{
    NSLog(@"draw rect called ");
   numberOfElement=[self.datasource numberOfObjectsInMarque:self];    
   timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(addElement) userInfo:nil repeats:YES];
}

//每次打印日志:

2013-01-22 17:23:36.680 Mob_Trading[2808:11303] draw rect called 
2013-01-22 17:23:36.882 Mob_Trading[2808:11303] add element called 
2013-01-22 17:23:36.884 Mob_Trading[2808:11303] subview frame is 320.000000
2013-01-22 17:23:36.884 Mob_Trading[2808:11303] add subview called
2013-01-22 17:23:37.082 Mob_Trading[2808:11303] add element called 
2013-01-22 17:23:37.083 Mob_Trading[2808:11303] subview frame is 320.000000
2013-01-22 17:23:37.282 Mob_Trading[2808:11303] add element called 
2013-01-22 17:23:37.283 Mob_Trading[2808:11303] subview frame is 320.000000
2013-01-22 17:23:37.482 Mob_Trading[2808:11303] add element called 
2013-01-22 17:23:37.483 Mob_Trading[2808:11303] subview frame is 320.000000
2013-01-22 17:23:37.682 Mob_Trading[2808:11303] add element called 
2013-01-22 17:23:37.683 Mob_Trading[2808:11303] subview frame is 320.000000
2013-01-22 17:23:37.882 Mob_Trading[2808:11303] add element called 
2013-01-22 17:23:37.883 Mob_Trading[2808:11303] subview frame is 320.000000
2013-01-22 17:23:38.082 Mob_Trading[2808:11303] add element called 
2013-01-22 17:23:38.083 Mob_Trading[2808:11303] subview frame is 320.000000

我也试过这段代码,但即使这样也行不通..

 CGRect projectileFrame = [[subView.layer presentationLayer] frame];
        NSLog(@"subview frame is %f",projectileFrame.origin.x);

提前致谢。我将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我制作了以下代码,并在动画

时返回x位置
#import "StockTiker.h"
#import "QuartzCore/QuartzCore.h"

static int count=0;

@implementation StockTiker

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        numberOfRows=0;
        // Initialization code
    }
    return self;
}

-(void)moveSubView
{
    UIView *subView=[self.delegate viewForRow:count];

    CABasicAnimation *theAnimation;
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    theAnimation.delegate=self;
    theAnimation.removedOnCompletion=YES;
    theAnimation.duration=5;
    theAnimation.repeatCount=1;
    theAnimation.autoreverses=NO;
    theAnimation.fromValue=[NSNumber numberWithFloat:320];
    theAnimation.toValue=[NSNumber numberWithFloat:-subView.frame.size.width];
    [subView.layer addAnimation:theAnimation forKey:@"animateLayer"];
}

- (CGRect)visibleRect
{
    CGRect visibleRect;
    UIView *view=[self.delegate viewForRow:count];
    visibleRect.origin = [view.layer.presentationLayer frame].origin;
    visibleRect.size = view.frame.size;
    return visibleRect;
}

-(void)addElement:(UIView*)subView
{
    if (![self.subviews containsObject:(id)subView]) {
        [self addSubview:subView];
        [self moveSubView];
    }
}

-(void)checkPosition
{
    float x=[self visibleRect].origin.x+[self visibleRect].size.width;
    NSLog(@"x position is %f",x);

    if (x<300) {
        count=count+1;
        if (count==numberOfRows) {
            count=0;
        }
    }

    UIView *subView=[self.delegate viewForRow:count];
    [self addElement:subView];
}

- (void)drawRect:(CGRect)rect
{    
    numberOfRows=[self.delegate numberOfRowsForStockTiker:self];
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(checkPosition) userInfo:nil repeats:YES];
}

#pragma mark- animation did stop selector

- (void)animationDidStop:(CABasicAnimation *)theAnimation finished:(BOOL)flag
{
    [[self.subviews objectAtIndex:0] removeFromSuperview];
}

@end

控制台--------------------

2013-01-29 09:36:46.371 Mob_Trading[297:11303] x position is 415.862579
2013-01-29 09:36:46.421 Mob_Trading[297:11303] x position is 411.661621
2013-01-29 09:36:46.472 Mob_Trading[297:11303] x position is 407.408386
2013-01-29 09:36:46.522 Mob_Trading[297:11303] x position is 403.188934
2013-01-29 09:36:46.571 Mob_Trading[297:11303] x position is 399.055939
2013-01-29 09:36:46.622 Mob_Trading[297:11303] x position is 394.804077
2013-01-29 09:36:46.672 Mob_Trading[297:11303] x position is 390.591888
2013-01-29 09:36:46.722 Mob_Trading[297:11303] x position is 386.388184
2013-01-29 09:36:46.771 Mob_Trading[297:11303] x position is 382.271362
2013-01-29 09:36:46.821 Mob_Trading[297:11303] x position is 378.072693
2013-01-29 09:36:46.871 Mob_Trading[297:11303] x position is 373.870941
2013-01-29 09:36:46.921 Mob_Trading[297:11303] x position is 369.670837
2013-01-29 09:36:46.971 Mob_Trading[297:11303] x position is 365.470734
2013-01-29 09:36:47.021 Mob_Trading[297:11303] x position is 361.269470
2013-01-29 09:36:47.072 Mob_Trading[297:11303] x position is 356.982086
2013-01-29 09:36:47.121 Mob_Trading[297:11303] x position is 352.858307
2013-01-29 09:36:47.172 Mob_Trading[297:11303] x position is 348.585815
2013-01-29 09:36:47.222 Mob_Trading[297:11303] x position is 344.443604
2013-01-29 09:36:47.271 Mob_Trading[297:11303] x position is 340.266754
2013-01-29 09:36:47.321 Mob_Trading[297:11303] x position is 336.064270
2013-01-29 09:36:47.371 Mob_Trading[297:11303] x position is 331.866699
2013-01-29 09:36:47.421 Mob_Trading[297:11303] x position is 327.658295
2013-01-29 09:36:47.472 Mob_Trading[297:11303] x position is 323.411072
2013-01-29 09:36:47.522 Mob_Trading[297:11303] x position is 319.199158
2013-01-29 09:36:47.572 Mob_Trading[297:11303] x position is 314.978210
2013-01-29 09:36:47.622 Mob_Trading[297:11303] x position is 310.785889
2013-01-29 09:36:47.672 Mob_Trading[297:11303] x position is 306.583618
2013-01-29 09:36:47.722 Mob_Trading[297:11303] x position is 302.382263
2013-01-29 09:36:47.772 Mob_Trading[297:11303] x position is 298.183167

答案 1 :(得分:0)

这里似乎没有任何问题,您的日志记录正在报告您应该期望的内容。

    [subView setFrame:CGRectMake(320, 0, 100, 100)];
    [subView setBackgroundColor:[UIColor blueColor]];

    NSLog(@"subview frame is %f",subView.frame.origin.x);

您可以使用origin.x = 320设置框架,然后将其记录回两行。

我有一种感觉,你希望做的是在子视图在屏幕上移动时看到origin的日志。如果是这样,你就不会走得太远,因为动画不会像那样工作。

动画不会逐步移动对象,重新绘制视图。在动画开始之前,对象被移动到它的最终位置 。因此,在动画期间的任何时候,如果你测试动画对象的位置,它将报告它的最终位置。