Google Play商店应用,例如Android中的评分栏

时间:2017-06-26 11:01:17

标签: android material-design ratingbar

我想创建一个与Google Play商店完全相同的评分栏,与下图相同,

enter image description here

我试过在github上找到多个库, 但仍然有一些问题,比如在一个星形大小是固定的,而在其他选定的开始边框颜色没有改变,

任何人都可以帮忙吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    databaseRef = Database.database().reference()

    locationManager.delegate = self
    locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    locationManager.stopUpdatingLocation()


    // refresh control
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(refreshControlAction(refreshControl:)), for: UIControlEvents.valueChanged)
    self.feedTableView.insertSubview(refreshControl, at: 0)

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if CLLocationManager.locationServicesEnabled() {
        switch(CLLocationManager.authorizationStatus()) {
        case .notDetermined, .restricted, .denied:
            print("No access")
            fetchPosts(refreshing: false, refreshControl: nil)
        //getAllPostsWithoutLocation(refreshing: false, refreshControl: nil)
        case .authorizedAlways, .authorizedWhenInUse:
            print("Access")
            fetchPostsWithLocation(refreshing: false, refreshControl: nil)
            //getAllPosts(refreshing: false, refreshControl: nil)
        }
    } else {
        print("Location services are not enabled")
    }

}


@objc func refreshControlAction(refreshControl: UIRefreshControl) {

    if CLLocationManager.locationServicesEnabled() {
        switch(CLLocationManager.authorizationStatus()) {
        case .notDetermined, .restricted, .denied:
            print("No access")
            fetchPosts(refreshing: true, refreshControl: refreshControl)
        //getAllPostsWithoutLocation(refreshing: true, refreshControl: refreshControl)
        case .authorizedAlways, .authorizedWhenInUse:
            print("Access")
            fetchPostsWithLocation(refreshing: true, refreshControl: refreshControl)
            //getAllPosts(refreshing: true, refreshControl: refreshControl)
        }
    } else {
        print("Location services are not enabled")
    }

}

func fetchPostsWithLocation(refreshing: Bool, refreshControl: UIRefreshControl?) {
    Database.database().reference().child("user_profiles").child((loggedInUser?.uid)!).child("following").observe(.value, with: { snapshot in
        if snapshot.exists() {
            MBProgressHUD.showAdded(to: self.view, animated: true)
            let databaseRef = Database.database().reference()
            // retrieves all users from database
            databaseRef.child("user_profiles").queryOrderedByKey().observeSingleEvent(of: .value, with: { (usersSnapshot) in
                let users = usersSnapshot.value as! [String: AnyObject]
                // retrieve user's following list and append it
                for (_, value) in users {
                    print(value)
                    if let userID = value["uid"] as? String {
                        if userID == Auth.auth().currentUser?.uid {
                            print(value)
                            if let followingUsers = value["following"] as? [String : String] {
                                for (_,user) in followingUsers {
                                    self.following.append(user)
                                }
                            }
                            // append user's id to see own posts
                            //self.following.append(Auth.auth().currentUser!.uid)
                            // retrieve all posts from the database
                            databaseRef.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (postsSnapshot) in
                                let posts = postsSnapshot.value as! [String: AnyObject]
                                // retrieve posts of each follower and user
                                for (_, post) in posts {
                                    for (_, postInfo) in post as! [String: AnyObject] {
                                        if let followingID = postInfo["uid"] as? String {
                                            for each in self.following {
                                                if each == followingID {
                                                    guard let uid = postInfo["uid"] as! String! else {return}
                                                    guard let caption = postInfo["caption"] as! String! else {return}
                                                    guard let downloadURL = postInfo["download_url"] as! String! else {return}
                                                    guard let name = postInfo["businessName"] as! String! else {return}
                                                    guard let timestamp = postInfo["timestamp"] as! Double! else {return}
                                                    let date = Date(timeIntervalSince1970: timestamp/1000)
                                                    guard let address = postInfo["businessStreet"] as! String! else {return}
                                                    guard let state = postInfo["businessCity"] as! String! else {return}
                                                    guard let postID = postInfo["postID"] as! String! else {return}

                                                    let lat = Double(postInfo["businessLatitude"] as! String)
                                                    let long = Double(postInfo["businessLongitude"] as! String)
                                                    let businessLocation = CLLocation(latitude: lat!, longitude: long!)

                                                    let latitude = self.locationManager.location?.coordinate.latitude
                                                    let longitude = self.locationManager.location?.coordinate.longitude
                                                    let userLocation = CLLocation(latitude: latitude!, longitude: longitude!)

                                                    let distanceInMeters: Double = userLocation.distance(from: businessLocation)
                                                    let distanceInMiles: Double = distanceInMeters * 0.00062137
                                                    let distanceLabelText = String(format: "%.2f miles away", distanceInMiles)

                                                    let post = Post(uid: uid, caption: caption, downloadURL: downloadURL, name: name, date: date, address: address, state: state, distance: distanceLabelText, postID: postID)

                                                    self.feeds.append(post)
                                                    self.tableView.reloadData()

                                                    self.refreshControl?.endRefreshing()
                                                }
                                                self.feeds.sort {$0.date.compare($1.date) == .orderedDescending}
                                                //self.feeds.sort {$0.distance.compare($1.distance) == .orderedAscending}

                                               self.tableView.reloadData()
                                            }
                                        }
                                    }
                                }
                                MBProgressHUD.hide(for: self.view, animated: true)
                            }) { (error) in
                                print(error.localizedDescription)
                            }
                        }
                    }
                }

            })
        } else {
            print("Not following anyone")
        }
    })
}

看起来像这样: enter image description here