2017-06-28 48 views
0

我在我的项目中有两个ScrollViews,他们应该同时滚动。当我滚动myFriendsScrollView时它应该工作。当第一个ScrollView滚动时滚动第二个滚动查看

这是我未完成的代码:

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 
    } 
} 

} 

感谢您的帮助。

+0

我认为这可以帮助你: - https://stackoverflow.com/questions/41131986/scrollview-gesture-recognizer垂直挥动 –

+0

尝试围绕主线程,.....请参阅下面的答案,在主线程 – Dhiru

回答

1

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

但是,它看起来并不像它实际上连接起来。请确保您的类是:

class profileController: UIViewController, UIScrollViewDelegate { ... } 

,并添加viewDidLoad

myFriendsScrollView.delegate = self 
myFriendsNameScrollView.delegate = self 
+0

thx中执行您的UI操作,它工作的很棒 – j10

0

你可以使用它的委托方法。 并设置内容抵消了其他滚动型

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



    } 

我希望这将帮助你