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.