【发布时间】: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
}
}
}
感谢您的帮助。
【问题讨论】:
-
我认为这可以帮助你:- stackoverflow.com/questions/41131986/…
-
尝试用主线程包围,.....看下面的答案,在主线程中做你的UI操作
标签: ios swift function swift3 uiscrollview