以编程方式滚动UIScrollView

时间:2010-02-10 06:29:41

标签: ios objective-c uiscrollview scroll

我有一个UIScrollView,它有几个视图。当用户轻拂手指时,视图会根据手指轻弹的方向向右或向左滚动。基本上我的代码的工作方式与iPhone照片应用程序类似。现在,有没有一种方法可以通过编程方式执行相同的操作,以便最终得到一个幻灯片,只需单击一个按钮就可以自行运行,并在每个滚动之间进行可配置的暂停?

你如何使用UIScrollView进行幻灯片演示?

10 个答案:

答案 0 :(得分:360)

您可以使用Objective-C

中的以下某个语句滚动到滚动视图中的某个点
[scrollView setContentOffset:CGPointMake(x, y) animated:YES];

或Swift

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

请参阅guide "Scrolling the Scroll View Content" from Apple as well

要使用UIScrollView进行幻灯片放映,您可以在滚动视图中排列所有图像,设置重复计时器,然后在计时器触发时-setContentOffset:animated:

但更有效的方法是使用2个图像视图并使用转换交换它们,或者只是在计时器触发时切换位置。有关详细信息,请参阅iPhone Image slideshow

答案 1 :(得分:37)

如果要控制动画的持续时间和样式,可以执行以下操作:

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
    scrollView.contentOffset = CGPointMake(x, y);
} completion:NULL];

调整持续时间(2.0f)和选项(UIViewAnimationOptionCurveLinear)以品尝!

答案 2 :(得分:10)

另一种方式是

scrollView.contentOffset = CGPointMake(x,y);

答案 3 :(得分:7)

使用Swift中的动画

scrollView.setContentOffset(CGPointMake(x, y), animated: true)

答案 4 :(得分:3)

Swift 3

   let point = CGPoint(x: 0, y: 200) // 200 or any value you like.
    scrollView.contentOffset = point

答案 5 :(得分:2)

scrollView.setContentOffset(CGPoint(x: y, y: x), animated: true)

答案 6 :(得分:2)

[Scrollview setContentOffset:CGPointMake(x, y) animated:YES];

答案 7 :(得分:1)

- (void)viewDidLoad
{
    [super viewDidLoad];
    board=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 80)];
    board.backgroundColor=[UIColor greenColor];
    [self.view addSubview:board];
    // Do any additional setup after loading the view.
}


-(void)viewDidLayoutSubviews
{


    NSString *str=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    index=1;
    for (int i=0; i<20; i++)
    {
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(-50, 15, 50, 50)];
        lbl.tag=i+1;
        lbl.text=[NSString stringWithFormat:@"%c",[str characterAtIndex:arc4random()%str.length]];
        lbl.textColor=[UIColor darkGrayColor];
        lbl.textAlignment=NSTextAlignmentCenter;
        lbl.font=[UIFont systemFontOfSize:40];
        lbl.layer.borderWidth=1;
        lbl.layer.borderColor=[UIColor blackColor].CGColor;
        [board addSubview:lbl];
    }

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

    NSLog(@"%d",[board subviews].count);
}


-(void)CallAnimation
{

    if (index>20) {
        index=1;
    }
    UIView *aView=[board viewWithTag:index];
    [self doAnimation:aView];
    index++;
    NSLog(@"%d",index);
}

-(void)doAnimation:(UIView*)aView
{
    [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear  animations:^{
        aView.frame=CGRectMake(self.view.frame.size.height, 15, 50, 50);
    }
                     completion:^(BOOL isDone)
     {
         if (isDone) {
             //do Somthing
                        aView.frame=CGRectMake(-50, 15, 50, 50);
         }
     }];
}

答案 8 :(得分:0)

我正在演示如何使用两个UIView创建一个简单的滚动视图,并添加一些自动布局约束,以便其平滑滚动。

我想您在main.storyboard中有一个空的视图控制器。

1。添加 UIView

ld = [[{'Instant_ID': 409597}, {'Token': 'You'}], [{'Instant_ID': 409597}, {'Token': 'matter'}], [{'Instant_ID': 409597}, {'Token': 'manager'}], [{'Instant_ID': 809558}, {'Token': 'metro'}], [{'Instant_ID': 809558}, {'Token': 'station'}], [{'Instant_ID': 829258}, {'Token': 'bucket'}], [{'Instant_ID': 829258}, {'Token': 'water'}]]
>>> [[e[1]['Token'] for e in v] for k,v in groupby(ld, lambda x: x[0]['Instant_ID'])]
[['You', 'matter', 'manager'], ['metro', 'station'], ['bucket', 'water']]

2。添加 ScrollView

let view = UIView()
    view.backgroundColor = UIColor.green
    self.view.addSubview(view)
    view.translatesAutoresizingMaskIntoConstraints = false
    top = view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0)
    left = view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0)
    right = view.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0)
    bottom = view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0)
    height = view.heightAnchor.constraint(equalToConstant: self.view.frame.height)
    NSLayoutConstraint.activate([top, right, left, height, bottom])

3。添加第一个 UILabel

let scroll = UIScrollView()
    scroll.backgroundColor = UIColor.red
    view.addSubview(scroll)
    scroll.translatesAutoresizingMaskIntoConstraints = false
    top = scroll.topAnchor.constraint(equalTo: view.topAnchor, constant: 10)
    left = scroll.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0)
    right = scroll.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0)
    bottom = scroll.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10)
    NSLayoutConstraint.activate([top, right, left, bottom])

4。添加第二个 UILabel

 let l2 = UILabel()
        l2.text = "Label1"
        l2.textColor = UIColor.black
        l2.backgroundColor = UIColor.blue
        scroll.addSubview(l2)
        l2.translatesAutoresizingMaskIntoConstraints = false
        top = l2.topAnchor.constraint(equalTo: scroll.topAnchor, constant: 10)
        left = l2.leadingAnchor.constraint(equalTo: scroll.leadingAnchor, constant: 10)
        height = l2.heightAnchor.constraint(equalToConstant: 100)
        right = l2.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10)
        NSLayoutConstraint.activate([top, left, right, height])

Label1 enter image description here

答案 9 :(得分:0)

这是另一个对我来说效果很好的用例。

  1. 用户点按按钮/单元格。
  2. 滚动到足以使目标视图可见的位置。

代码:Swift 5.3

// Assuming you have a view named "targeView"
scrollView.scroll(to: CGPoint(x:targeView.frame.minX, y:targeView.frame.minY), animated: true)

如您所想,如果您想滚动以显示目标视图的底部,请使用 maxX 和 minY。