0

アニメーションでビューを移動しているときに、ログを印刷して移動要素の 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);

よろしくお願いします。

4

2 に答える 2

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
于 2013-01-29T04:05:46.043 に答える
0

ここには問題はないようです。ログは期待どおりに報告されています。

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

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

フレームを origin.x=320 で設定し、2 行後に記録します。

サブビューが画面上を移動するにつれて、origin.x のログが時間の経過とともに変化するのを確認することを望んでいるように感じますか? もしそうなら、アニメーションはそのようには機能しないので、あなたはそれほど遠くまで行くつもりはありません.

アニメーションはオブジェクトを段階的に移動せず、進行に合わせてビューを再描画します。オブジェクトは、アニメーションが開始する前に最終的な位置に移動されます。したがって、アニメーション中の任意の時点で、アニメーション化されたオブジェクトの位置をテストすると、最終的な位置が報告されます。

于 2013-01-22T12:14:59.803 に答える