以编程方式滚动UIScrollView

问题描述 投票:138回答:10

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

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

ios objective-c uiscrollview scroll
10个回答
368
投票

您可以使用Objective-C中的以下语句之一滚动到滚动视图中的某个点

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

或斯威夫特

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


0
投票

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

我假设你在main.storyboard中有一个空的视图控制器。

1.添加UIView。

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])

2.添加ScrollView。

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])

3.添加第一个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])

4.添加第二个UILabel。

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

Label1 enter image description here


38
投票

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

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

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


11
投票

另一种方式是

scrollView.contentOffset = CGPointMake(x,y);

9
投票

在Swift中使用动画

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

3
投票

斯威夫特3

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

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

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

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);
         }
     }];
}

1
投票

我很惊讶这个话题已经有9年了,实际的直截了当的答案不在这里!

你要找的是scrollRectToVisible(_:animated:)

例:

extension SignUpView: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        scrollView.scrollRectToVisible(textField.frame, animated: true)
    }
}

它的作用正是你需要的,它比hacky contentOffset好得多

此方法滚动内容视图,以便rect定义的区域在滚动视图中可见。如果该区域已经可见,则该方法不执行任何操作。

来自:https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible

© www.soinside.com 2019 - 2024. All rights reserved.