简体   繁体   中英

x position not changing while animating UIView in motion?

I have following lines of code in which while moving view with animation I checked out x position of moving element by printing log.It is showing same x position every time while moving view.

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

//print log every time:

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

I have also tried this code but even this also not working..

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

thanks in advance .I will appreciate your help.

I have made this following code and it is returning x position while animating

#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

console--------------------

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

There doesn't seem to be any problem here, your logging is reporting exactly what you should expect.

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

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

You set the frame with origin.x=320, and you log that back two lines later.

I have a feeling that what you are hoping to do is to see a log of origin.x changing over time as the subview moves across the screen? If so, you are not going to get very far, as animations don't work like that.

An animation does not move an object incrementally, redrawing the view as it goes. The object is moved to it's final position before the animation starts . So at any point during the animation, if you test the position of the animated object it will report it's final position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM