Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用NSTimer在两个UIImageView之间逐个切换_Ios_Objective C_Uiimageview_Nstimer - Fatal编程技术网

Ios 使用NSTimer在两个UIImageView之间逐个切换

Ios 使用NSTimer在两个UIImageView之间逐个切换,ios,objective-c,uiimageview,nstimer,Ios,Objective C,Uiimageview,Nstimer,我见过类似的问题,但给出的答案都不适用于我的情况。我想给我的角色一个他正在行走的错觉,但他实际上会保持在相同的坐标上,而背景会移动,我想这样做的方式是使用iAction启动我的计时器,到目前为止,我的代码是这样的 -(IBAction)StartGame:(id)sender { PersonMovement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(PersonMo

我见过类似的问题,但给出的答案都不适用于我的情况。我想给我的角色一个他正在行走的错觉,但他实际上会保持在相同的坐标上,而背景会移动,我想这样做的方式是使用
iAction
启动我的计时器,到目前为止,我的代码是这样的

-(IBAction)StartGame:(id)sender
{    
    PersonMovement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(PersonMovement) userInfo:nil repeats:YES];
}

现在使用该计时器,每隔0.05秒,我希望我的人的
UIImageView
在(step1.png和step2.png)之间来回切换。

您应该使用
UIImageView
s
animationImages
animationDuration
属性来完成此交换

您的
animationImages
数组将是您的两个图像,您的
animationDuration
将是
0.1
以在0.1秒的时间跨度内显示这两个图像。然后调用
startAnimating
启动序列


显示您尝试切换图像的
PersonMovement
方法的实现。@rmaddy那里没有什么我不知道我到底在做什么,所以我已经准备好了,可以将代码放入其中。您能诚实地给我一个示例代码吗?这不管用。。。这是我的-(void)PersonMovement{UIImageView*step1;NSArray*imagesArray=[NSArray arrayWithObjects:[UIImage ImageName:@“step1.png”],[UIImage ImageName:@“step2.png”],[UIImage ImageName:@“step2.png”],nil];step1=[UIImageView alloc]initWithImage:[UIImage ImageName:@“step1.png”];[step1设置动画图像:imagesArray];[step1 setAnimationDuration:0.3];[step1 setAnimationRepeatCount:100];[step1 startAnimating];}我添加了一个我刚刚测试过的示例。看起来你的代码没有创建UIImageView并将其添加到视图层次结构中。太棒了!非常感谢你,现在还有最后一件事,我该如何更改图像的大小?正在设置动画的图像比它们应该的大得多…如果你所有的图像都是相同的大小,那么设置大小创建
UIImageView
CGRectMake时的帧(0.0f、20.0f、宽度、高度)
。如果您的图像大小不同,请将大小设置为最大,然后设置
imageView.contentMode=UIViewContentModeCenter;
。谢谢@Neil,既然您帮了我这么多,我想再帮一点忙?我正在尝试制作一个触摸事件,将我的动画对象向上移动10像素。这是我尝试过的,但没有成功…我f(UITouchPhaseBegan){imageView=[[UIImageView alloc]initWithFrame:cDirectMake(10,407+10,45,45)];}如果(UITouchPhaseEded){imageView=[[UIImageView alloc]initWithFrame:cDirectMake(10,407-10,45,45)];}有什么想法吗?我还试着简单地沉思一下-(void)触摸开始…*并尝试在那里编写代码,但我没有运气…谢谢你的帮助!
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image1 = [UIImage imageNamed:@"image1"];
    UIImage *image2 = [UIImage imageNamed:@"image2"];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 200.0f)];
    [self.view addSubview:imageView];

    imageView.animationImages = @[image1, image2];
    imageView.animationDuration = 0.1;
    [imageView startAnimating];
}