毎度お世話になります。
マッチングアプリによくある、ユーザーギャラリーにニックネームや地域、年齢などを表示させるビューを作成しています。
画像単体で表示させる分にはスクロールがスムーズなのですが、その上にUILabelを表示させると極端にスクロールが重くなります。
ラベルには文字を表示させてもさせなくてももっさり感は変わりません。
これを解消させる手段が思いつかず、Web上にある情報をいろいろ試しましたが思い通りの結果になりません。
おわかりになる方がいらっしゃいましたらご教授いただけると助かります。
よろしくお願いいたします。
ViewController.swift
swift
1func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 2 3 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UsersCell", forIndexPath: indexPath) as! UsersCollectionViewCell 4 cell.setupCustomCellImage() 5 cell.setupCustomCellLabel() 6 cell.imageConstraint() 7 cell.labelConstraint() 8 cell.setupContents(indexPath.row, items: self.userItems[indexPath.row]) //self.userItemsはAPIからデータを取得 9 10 return cell 11}
UsersCollectionViewCell.swift
swift
1class UsersCollectionViewCell: UICollectionViewCell { 2 3 let imageView = UIImageView() 4 let statusLabel = CustomLabelClass() 5 6 required init(coder aDecoder: NSCoder) { 7 super.init(coder: aDecoder)! 8 } 9 10 override init(frame: CGRect) { 11 super.init(frame: frame) 12 13 self.addSubview(imageView) 14 self.addSubview(statusLabel) 15 } 16 17 func setupCustomCellImage() { 18 self.backgroundColor = UIColor.whiteColor() 19 self.imageView.contentMode = UIViewContentMode.ScaleToFill 20 self.imageView.layer.cornerRadius = 6 21 self.imageView.layer.masksToBounds = true 22 } 23 24 func imageConstraint() { 25 // 画像の制約 26 } 27 28 func setupCustomCellLabel() { 29 self.statusLabel.textAlignment = NSTextAlignment.Center 30 self.statusLabel.backgroundColor = UIColor.rgb(0x000000, 0.8) 31 self.statusLabel.backgroundColor = UIColor.blackColor() 32 self.statusLabel.setCornersAndRadius([.BottomLeft, .BottomRight], radius: 6.0) 33 self.statusLabel.font = UIFont.systemFontOfSize(CGFloat(10)) 34 self.statusLabel.textColor = UIColor.orangeColor() 35 } 36 37 func labelConstraint() { 38 // ラベルの制約 39 } 40 41 // コンテンツの表示 42 func setupContents(num: Int, items: Dictionary<String, AnyObject>) { 43 statusLabel.text = "ニックネーム" 44 if items["image_url"] as! String != "" { 45 imageView.sd_setImageWithURL(NSURL(string: "http://hogehoge.com" + (items["image_url"] as! String))) 46 } else { 47 imageView.image = UIImage(named: "dummy.jpg") 48 } 49 } 50 51}
追記:
ご指摘のあったCustomLabelClass.swiftの内容を載せてみました。
内容は、個々でラベルのコーナーをラウンドさせるための処理になります。
swift
1class CustomLabelClass: UILabel { 2 3 var corners: UIRectCorner = [.TopLeft, .TopRight, .BottomLeft, .BottomRight] 4 var radius: CGFloat = 0 5 6 var padding: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 7 8 func setPadding(pt:CGFloat, pr:CGFloat, pb:CGFloat, pl:CGFloat) { 9 self.padding = UIEdgeInsets(top: pt, left: pl, bottom: pb, right: pr) 10 } 11 12 override func drawTextInRect(rect: CGRect) { 13 let newRect = UIEdgeInsetsInsetRect(rect, padding) 14 super.drawTextInRect(newRect) 15 } 16 17 override func intrinsicContentSize() -> CGSize { 18 var intrinsicContentSize = super.intrinsicContentSize() 19 intrinsicContentSize.height += padding.top + padding.bottom 20 intrinsicContentSize.width += padding.left + padding.right 21 return intrinsicContentSize 22 } 23 24 func setCornersAndRadius(corners:UIRectCorner, radius: CGFloat) { 25 self.corners = corners 26 self.radius = radius 27 } 28 29 override func layoutSubviews() { 30 super.layoutSubviews() 31 let maskPath = UIBezierPath(roundedRect: self.bounds, 32 byRoundingCorners: self.corners, 33 cornerRadii: CGSize(width: self.radius, height: self.radius)) 34 35 let maskLayer = CAShapeLayer() 36 maskLayer.frame = self.bounds 37 maskLayer.path = maskPath.CGPath 38 39 self.layer.mask = maskLayer 40 } 41}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/24 01:19
2016/05/24 14:19