Skip to content

Commit 0d15917

Browse files
committed
更新 demo
1 parent b813a29 commit 0d15917

6 files changed

Lines changed: 147 additions & 27 deletions

File tree

CornerRadius/CornerRadius.xcodeproj/project.pbxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
9B49155B1C81D6080008161A /* CornerRadiusTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B49155A1C81D6080008161A /* CornerRadiusTests.swift */; };
1616
9B4915661C81D6080008161A /* CornerRadiusUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B4915651C81D6080008161A /* CornerRadiusUITests.swift */; };
1717
9B4915741C81D77D0008161A /* CustomTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B4915731C81D77D0008161A /* CustomTableViewCell.swift */; };
18+
9BADBA5E1C82B1B3000DB0E2 /* KtCorner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BADBA5D1C82B1B3000DB0E2 /* KtCorner.swift */; };
1819
/* End PBXBuildFile section */
1920

2021
/* Begin PBXContainerItemProxy section */
@@ -49,6 +50,7 @@
4950
9B4915651C81D6080008161A /* CornerRadiusUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CornerRadiusUITests.swift; sourceTree = "<group>"; };
5051
9B4915671C81D6080008161A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5152
9B4915731C81D77D0008161A /* CustomTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomTableViewCell.swift; sourceTree = "<group>"; };
53+
9BADBA5D1C82B1B3000DB0E2 /* KtCorner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KtCorner.swift; sourceTree = "<group>"; };
5254
/* End PBXFileReference section */
5355

5456
/* Begin PBXFrameworksBuildPhase section */
@@ -102,6 +104,7 @@
102104
9B4915451C81D6080008161A /* AppDelegate.swift */,
103105
9B4915471C81D6080008161A /* ViewController.swift */,
104106
9B4915731C81D77D0008161A /* CustomTableViewCell.swift */,
107+
9BADBA5D1C82B1B3000DB0E2 /* KtCorner.swift */,
105108
9B4915491C81D6080008161A /* Main.storyboard */,
106109
9B49154C1C81D6080008161A /* Assets.xcassets */,
107110
9B49154E1C81D6080008161A /* LaunchScreen.storyboard */,
@@ -263,6 +266,7 @@
263266
buildActionMask = 2147483647;
264267
files = (
265268
9B4915741C81D77D0008161A /* CustomTableViewCell.swift in Sources */,
269+
9BADBA5E1C82B1B3000DB0E2 /* KtCorner.swift in Sources */,
266270
9B4915481C81D6080008161A /* ViewController.swift in Sources */,
267271
9B4915461C81D6080008161A /* AppDelegate.swift in Sources */,
268272
);
@@ -489,6 +493,7 @@
489493
9B49156C1C81D6080008161A /* Release */,
490494
);
491495
defaultConfigurationIsVisible = 0;
496+
defaultConfigurationName = Release;
492497
};
493498
9B49156D1C81D6080008161A /* Build configuration list for PBXNativeTarget "CornerRadiusTests" */ = {
494499
isa = XCConfigurationList;
@@ -497,6 +502,7 @@
497502
9B49156F1C81D6080008161A /* Release */,
498503
);
499504
defaultConfigurationIsVisible = 0;
505+
defaultConfigurationName = Release;
500506
};
501507
9B4915701C81D6080008161A /* Build configuration list for PBXNativeTarget "CornerRadiusUITests" */ = {
502508
isa = XCConfigurationList;
@@ -505,6 +511,7 @@
505511
9B4915721C81D6080008161A /* Release */,
506512
);
507513
defaultConfigurationIsVisible = 0;
514+
defaultConfigurationName = Release;
508515
};
509516
/* End XCConfigurationList section */
510517
};

CornerRadius/CornerRadius/Base.lproj/LaunchScreen.storyboard

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
56
</dependencies>
67
<scenes>
78
<!--View Controller-->
@@ -15,7 +16,6 @@
1516
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
1617
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1718
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18-
<animations/>
1919
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
2020
</view>
2121
</viewController>

CornerRadius/CornerRadius/CustomTableViewCell.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,27 @@ import UIKit
1010

1111
class CustomTableViewCell: UITableViewCell {
1212

13-
let imgView1 = UIImageView(frame: CGRectMake(20, 20, 60, 61))
14-
let imgView2 = UIImageView(frame: CGRectMake(100, 20, 60, 61))
15-
let imgView3 = UIImageView(frame: CGRectMake(180, 20, 60, 61))
16-
let imgView4 = UIImageView(frame: CGRectMake(260, 20, 60, 61))
13+
let imgView1 = UIImageView(frame: CGRectMake(5, 10, 20, 21))
14+
let imgView2 = UIImageView(frame: CGRectMake(55, 10, 20, 21))
15+
let view = UIView(frame: CGRectMake(150,10,40,21))
16+
let label = UILabel(frame: CGRectMake(250,10,80,21))
1717

1818
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
1919
super.init(style: style, reuseIdentifier: reuseIdentifier)
2020

21-
imgView1.layer.cornerRadius = 30
22-
imgView1.layer.masksToBounds = true
23-
24-
imgView2.layer.cornerRadius = 30
25-
imgView2.layer.masksToBounds = true
21+
self.contentView.addSubview(imgView1)
22+
self.contentView.addSubview(imgView2)
23+
24+
view.backgroundColor = UIColor.brownColor()
25+
view.layer.cornerRadius = 5
2626

27-
imgView3.layer.cornerRadius = 30
28-
imgView3.layer.masksToBounds = true
27+
self.contentView.addSubview(view)
2928

30-
imgView4.layer.cornerRadius = 30
31-
imgView4.layer.masksToBounds = true
3229

33-
self.contentView.addSubview(imgView1)
34-
self.contentView.addSubview(imgView2)
35-
self.contentView.addSubview(imgView3)
36-
self.contentView.addSubview(imgView4)
30+
label.kt_addCorner(radius: 8)
31+
// label.layer.cornerRadius = 5
32+
label.text = "123"
33+
self.contentView.addSubview(label)
3734
}
3835

3936
required init?(coder aDecoder: NSCoder) {
@@ -48,8 +45,10 @@ class CustomTableViewCell: UITableViewCell {
4845
func setupContent(imgName imgName: String) {
4946
imgView1.image = UIImage(named: imgName)
5047
imgView2.image = UIImage(named: imgName)
51-
imgView3.image = UIImage(named: imgName)
52-
imgView4.image = UIImage(named: "photo")
48+
49+
// 下面两行取消注释后试试
50+
// imgView1.kt_addCorner(radius: 5)
51+
// imgView2.kt_addCorner(radius: 5)
5352
}
5453

5554
override func setSelected(selected: Bool, animated: Bool) {

CornerRadius/CornerRadius/Info.plist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
<key>UISupportedInterfaceOrientations</key>
3434
<array>
3535
<string>UIInterfaceOrientationPortrait</string>
36-
<string>UIInterfaceOrientationLandscapeLeft</string>
37-
<string>UIInterfaceOrientationLandscapeRight</string>
3836
</array>
3937
</dict>
4038
</plist>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// KtCorner.swift
3+
// CornerRadius
4+
//
5+
// Created by 张星宇 on 16/2/28.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import UIKit
11+
12+
private func roundbyunit(num: Double, inout _ unit: Double) -> Double {
13+
let remain = modf(num, &unit)
14+
if (remain > unit / 2.0) {
15+
return ceilbyunit(num, &unit)
16+
} else {
17+
return floorbyunit(num, &unit)
18+
}
19+
}
20+
private func ceilbyunit(num: Double, inout _ unit: Double) -> Double {
21+
return num - modf(num, &unit) + unit
22+
}
23+
24+
private func floorbyunit(num: Double, inout _ unit: Double) -> Double {
25+
return num - modf(num, &unit)
26+
}
27+
28+
private func pixel(num: Double) -> Double {
29+
var unit: Double
30+
switch Int(UIScreen.mainScreen().scale) {
31+
case 1: unit = 1.0 / 1.0
32+
case 2: unit = 1.0 / 2.0
33+
case 3: unit = 1.0 / 3.0
34+
default: unit = 0.0
35+
}
36+
return roundbyunit(num, &unit)
37+
}
38+
39+
extension UIView {
40+
func kt_addCorner(radius radius: CGFloat) {
41+
self.kt_addCorner(radius: radius, borderWidth: 1, backgroundColor: UIColor.clearColor(), borderColor: UIColor.blackColor())
42+
}
43+
44+
func kt_addCorner(radius radius: CGFloat,
45+
borderWidth: CGFloat,
46+
backgroundColor: UIColor,
47+
borderColor: UIColor) {
48+
let imageView = UIImageView(image: kt_drawRectWithRoundedCorner(radius: radius,
49+
borderWidth: borderWidth,
50+
backgroundColor: backgroundColor,
51+
borderColor: borderColor))
52+
self.insertSubview(imageView, atIndex: 0)
53+
}
54+
55+
func kt_drawRectWithRoundedCorner(radius radius: CGFloat,
56+
borderWidth: CGFloat,
57+
backgroundColor: UIColor,
58+
borderColor: UIColor) -> UIImage {
59+
let sizeToFit = CGSize(width: pixel(Double(self.bounds.size.width)), height: Double(self.bounds.size.height))
60+
let halfBorderWidth = CGFloat(borderWidth / 2.0);
61+
62+
UIGraphicsBeginImageContextWithOptions(sizeToFit, false, UIScreen.mainScreen().scale)
63+
let context = UIGraphicsGetCurrentContext()
64+
65+
CGContextSetLineWidth(context, borderWidth);
66+
CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
67+
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
68+
69+
let width = sizeToFit.width, height = sizeToFit.height
70+
CGContextMoveToPoint(context, width - halfBorderWidth, radius + halfBorderWidth); // 开始坐标右边开始
71+
CGContextAddArcToPoint(context, width - halfBorderWidth, height - halfBorderWidth, width - radius - halfBorderWidth, height - halfBorderWidth, radius); // 右下角角度
72+
CGContextAddArcToPoint(context, halfBorderWidth, height - halfBorderWidth, halfBorderWidth, height - radius - halfBorderWidth, radius); // 左下角角度
73+
CGContextAddArcToPoint(context, halfBorderWidth, halfBorderWidth, width - halfBorderWidth, halfBorderWidth, radius); // 左上角
74+
CGContextAddArcToPoint(context, width - halfBorderWidth, halfBorderWidth, width - halfBorderWidth, radius + halfBorderWidth, radius); // 右上角
75+
76+
CGContextDrawPath(UIGraphicsGetCurrentContext(), .FillStroke)
77+
let output = UIGraphicsGetImageFromCurrentImageContext();
78+
UIGraphicsEndImageContext();
79+
return output
80+
}
81+
}
82+
83+
extension UIImageView {
84+
/**
85+
/ !!!只有当 imageView 不为nil 时,调用此方法才有效果
86+
87+
:param: radius 圆角半径
88+
*/
89+
override func kt_addCorner(radius radius: CGFloat) {
90+
self.image = self.image?.kt_drawRectWithRoundedCorner(radius: radius, self.bounds.size)
91+
}
92+
}
93+
94+
extension UIImage {
95+
func kt_drawRectWithRoundedCorner(radius radius: CGFloat, _ sizetoFit: CGSize) -> UIImage {
96+
let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: sizetoFit)
97+
98+
UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.mainScreen().scale)
99+
CGContextAddPath(UIGraphicsGetCurrentContext(),
100+
UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner.AllCorners,
101+
cornerRadii: CGSize(width: radius, height: radius)).CGPath)
102+
CGContextClip(UIGraphicsGetCurrentContext())
103+
104+
self.drawInRect(rect)
105+
CGContextDrawPath(UIGraphicsGetCurrentContext(), .FillStroke)
106+
let output = UIGraphicsGetImageFromCurrentImageContext();
107+
UIGraphicsEndImageContext();
108+
109+
return output
110+
}
111+
}

CornerRadius/CornerRadius/ViewController.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
1717
table.dataSource = self
1818

1919
view.addSubview(table)
20+
21+
let imageView = UIImageView(image: UIImage(named: "photo"))
22+
imageView.frame = CGRectMake(100, 100, 200, 200)
23+
imageView.kt_addCorner(radius: 20)
24+
// view.addSubview(imageView)
2025
// Do any additional setup after loading the view, typically from a nib.
2126
}
2227

@@ -33,17 +38,17 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
3338
cell?.selectionStyle = .None
3439
}
3540

36-
cell?.setupContent(imgName: String(indexPath.row + 1))
41+
cell?.setupContent(imgName: "photo")
3742

3843
return cell!
3944
}
4045

4146
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
42-
return 200
47+
return 40
4348
}
4449

4550
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
46-
return 10
51+
return 100
4752
}
4853
}
4954

0 commit comments

Comments
 (0)