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