【问题标题】:Scroll second Scrollview when first ScrollView did scroll当第一个 ScrollView 滚动时滚动第二个 Scrollview
【发布时间】:2017-12-01 17:03:14
【问题描述】:

我的项目中有两个ScrollViews,它们应该同时滚动。当我滚动myFriendsScrollView 时它应该可以工作。

这是我未完成的代码:

import Foundation
import UIKit

class profileController: UIViewController {

@IBOutlet weak var myFriendsScrollView: UIScrollView!
@IBOutlet weak var myFriendsNameScrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    // My Scrollview One
    let myFriendsScrollViewHeight = self.myFriendsScrollView.frame.height
    let myFriendsScrollViewWidth = self.myFriendsScrollView.frame.width

    let myFriendImg1 = UIImageView(frame: CGRect(x: 0, y: 0, width: myFriendsScrollViewWidth/4, height: myFriendsScrollViewHeight))
    let myFriendImg2 = UIImageView(frame: CGRect(x: 100 , y: 0, width: myFriendsScrollViewWidth/4, height: myFriendsScrollViewHeight))  
    let myFriendImg3 = UIImageView(frame: CGRect(x: 200 , y: 0, width: myFriendsScrollViewWidth/4, height: myFriendsScrollViewHeight))

    self.myFriendsScrollView.addSubview(myFriendImg1)
    self.myFriendsScrollView.addSubview(myFriendImg2)
    self.myFriendsScrollView.addSubview(myFriendImg3)

    myFriendImg1.image = UIImage(named: "avatar_placeholder");
    myFriendImg2.image = UIImage(named: "avatar_placeholder");
    myFriendImg3.image = UIImage(named: "avatar_placeholder");


    self.myFriendsScrollView.contentSize = CGSize(width: self.myFriendsScrollView.frame.width*2, height: self.myFriendsScrollView.frame.height)
    self.myFriendsScrollView.isPagingEnabled = true

    // My Scrollview Two
    let myFriendsNameScrollViewHeight = self.myFriendsNameScrollView.frame.height
    let myFriendsNameScrollViewWidth = self.myFriendsNameScrollView.frame.width

    let myFriendLbl1 = UILabel(frame: CGRect(x: 0, y: 0, width: myFriendsNameScrollViewWidth/4, height: myFriendsNameScrollViewHeight))
    let myFriendLbl2 = UILabel(frame: CGRect(x: 100 , y: 0, width: myFriendsNameScrollViewWidth/4, height: myFriendsNameScrollViewHeight))
    let myFriendLbl3 = UILabel(frame: CGRect(x: 200 , y: 0, width: myFriendsNameScrollViewWidth/4, height: myFriendsNameScrollViewHeight))

    self.myFriendsNameScrollView.addSubview(myFriendLbl1)
    self.myFriendsNameScrollView.addSubview(myFriendLbl2)
    self.myFriendsNameScrollView.addSubview(myFriendLbl3)

    myFriendLbl1.text = "UserName";
    myFriendLbl2.text = "UserName";
    myFriendLbl3.text = "UserName";

    self.myFriendsNameScrollView.contentSize = CGSize(width: self.myFriendsNameScrollView.frame.width*2, height: self.myFriendsNameScrollView.frame.height)
    self.myFriendsNameScrollView.isPagingEnabled = true

}  

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == myFriendsScrollView {
        myFriendsNameScrollView.contentOffset = scrollView.contentOffset
    }
    else {
        myFriendsScrollView.contentOffset = scrollView.contentOffset
    }
}

}

感谢您的帮助。

【问题讨论】:

标签: ios swift function swift3 uiscrollview


【解决方案1】:

你可以使用它的委托方法。 并在其他 ScrollView 上设置内容 OffSet

  func scrollViewDidScroll(_ scrollView: UIScrollView!) {
            // This will be called every time the user scrolls the scroll view 

       if  myFriendsScrollView ==scrollView 
        {
           let contentOffSet = scrollView.contentOffset

         //change the Off set of other ScrollView   
     // just surround with main thread 

      DispatchQueue.main.async {
         secondScrollView.setContentOffset=contentOffSet;
       }
    }



  }

希望对你有帮助

【讨论】:

    【解决方案2】:

    您的委托方法的实现似乎已经正确,我已经在操场上进行了测试,它似乎工作得很好。

    但是,它看起来并没有真正连接起来。确保您的班级是:

    class profileController: UIViewController, UIScrollViewDelegate { ... }
    

    并添加viewDidLoad:

    myFriendsScrollView.delegate = self
    myFriendsNameScrollView.delegate = self
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多