Создание UITableViewCell, который закруглен, но только на одном конце - PullRequest
0 голосов
/ 04 июня 2018

У меня проблема в Свифте.Я пытаюсь сделать tableViewCell, который округляется, но только на одном конце.Я знаю, как сделать закругленные tableViewCells с cornerRadius, но я хочу, чтобы эффект применялся только к началу tableViewCell, а конец был стандартным.

This is basically what I am trying to achieve

на данный момент с расширениями кода ниже я получаю это: currently what I am getting is this my cell is getting clipped

import UIKit
import SwipeCellKit

class CategoryTableViewCell: SwipeTableViewCell {

    @IBOutlet weak var categoryNameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        roundCorners([.topLeft, .bottomLeft], radius: self.bounds.height / 2)
}

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
    }
}

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

Ответы [ 2 ]

0 голосов
/ 04 июня 2018

Добавить расширение к UIView:

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

Теперь вы можете сделать закругленный угол любого угла просмотра, как показано в примере ниже.

myView.roundCorners([.topLeft,.topRight,.bottomRight], radius: 10)
0 голосов
/ 04 июня 2018

сначала вы должны решить, какую из них округлить.

extension UIView {
    func bottomRoundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }

    func topRoundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

Я буду применять пример кода класса collectionViewCell следующим образом

func configureCell(withBottomCorner bottomCorner: Bool) {
    if bottomCorner == true {
        self.contentView.clipsToBounds = true
        setBottomCorner()
   }
}

func setBottomCorner() {
   if #available(iOS 11.0, *) {
      self.contentView.layer.cornerRadius = 6
        self.contentView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
    } else {
        self.contentView.bottomRoundCorners([.bottomLeft, .bottomRight], radius: 6.0)
    }
 }

надеюсь, что это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...