gpt4 book ai didi

iphone - 在 UIScrollview 上停止动画

转载 作者:行者123 更新时间:2023-11-28 22:45:38 24 4
gpt4 key购买 nike

我设置了一些代码以在 TimeInterval 后自动滚动(分页) ScrollView 。现在我想在 4 次后停止 ScrollView 动画。有人可以教我怎么做吗?

这是我的代码。

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

- (void) onTimer {

// Updates the variable h, adding 320
abc += 320;

//This makes the scrollView scroll to the desired position
[UIView animateWithDuration:1.5f animations:^{
[scrollView setContentOffset:CGPointMake(abc, 0) animated:NO];
}];
}

最佳答案

首先给NSTimer添加一个ivar

{
NSTimer *timer;
}

timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

然后在onTimer

- (void) onTimer {
//Create a static int
static int repetition = 0;
repetition ++;
if(repetition == 4)
{
repetition = 0;
//Stop the timer
[timer invalidate];
}

// Updates the variable h, adding 320
abc += 320;

//This makes the scrollView scroll to the desired position
[UIView animateWithDuration:1.5f animations:^{
[scrollView setContentOffset:CGPointMake(abc, 0)animated:NO];


}];
}

关于iphone - 在 UIScrollview 上停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13276227/

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