Reputation: 109
I have two ScrollViews
in my Project and they should scroll at the same time. It should work when I scroll the myFriendsScrollView
.
That is my unfinished code:
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
}
}
}
Thanks for your help.
Upvotes: 0
Views: 302
Reputation: 315
It seems like your implementation of the delegate method is correct already, I've tested in playground and it seems to work great.
However, it doesn't look like it's actually hooked up. Make sure that your class is:
class profileController: UIViewController, UIScrollViewDelegate { ... }
and add in viewDidLoad
:
myFriendsScrollView.delegate = self
myFriendsNameScrollView.delegate = self
Upvotes: 1
Reputation: 3060
you can use its delegate method . and set the content OffSet on other ScrollView
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;
}
}
}
i hope this will help you
Upvotes: 1