gpt4 book ai didi

ios - 在运动中为 UIView 设置动画时 x 位置没有改变?

转载 作者:行者123 更新时间:2023-11-28 22:41:18 25 4
gpt4 key购买 nike

我有以下几行代码,其中在使用动画移动 View 时,我通过打印日志检查了移动元素的 x 位置。移动 View 时每次都显示相同的 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);

提前致谢。感谢您的帮助。

最佳答案

我制作了以下代码,它在动画时返回 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

关于ios - 在运动中为 UIView 设置动画时 x 位置没有改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14457902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com