设置根控制器
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow.init(frame: UIScreen.main.bounds)
let viewController = ViewController()
let navigationController = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
}
UIView
有关UIView的设置,背景、层级、大小、指针地址
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blue
var view1 = UIView(frame: CGRect(x:0,y:0,width: 100,height: 100))
view1.backgroundColor = UIColor.gray
self.view.addSubview(view1)
let p3 = withUnsafePointer(to: &view1){ptr in
return ptr}
print("view3的指针地址为:\(p3)" )
self.view.layer.contents = UIImage(named:"image")?.cgImage
}
得到屏幕大小
let yourFrame = UIScreen.main.bounds
打开新的控制器
func openViewController(){
let newViewController = yUIView()
self.present(newViewController,animated: true,completion: nil)
}
UILabel
func yUILabel() {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.backgroundColor = UIColor.black
label.alpha = 0.5
label.text = "Hello,world!"
label.textColor = UIColor.white
label.font = UIFont(name: "ZapFino", size:20)
label.textAlignment = NSTextAlignment.center
label.shadowColor = UIColor.gray
label.shadowOffset = CGSize(width: -5,height: 5)
label.layer.cornerRadius = 20
label.layer.borderColor = (CGColor)(red: 0, green: 255, blue: 0, alpha: 1)
label.layer.borderWidth = 6
}
UITextField
func yUITextField(){
let textField = UITextField(frame: CGRect(x: 75, y: 100, width: 200, height: 100))
textField.backgroundColor = UIColor.white
textField.textColor = UIColor.red
textField.textAlignment = . center
textField.contentVerticalAlignment = . center
textField.font = UIFont(name: "textfield", size: 14)
textField.placeholder = "请输入密码"
textField.isSecureTextEntry = true
textField.returnKeyType = UIReturnKeyType.next
textField.clearsOnBeginEditing = true
}
UIButton
func yUIButton() {
let button = UIButton(type: .custom)
button.frame = CGRect(x: 75, y: 100, width: 100, height: 50)
button.setTitle("点我", for: .normal)
button.backgroundColor = UIColor.yellow
button.setTitleColor(UIColor.orange, for: .normal)
button.setBackgroundImage(UIImage(named: "Image"), for: .normal)
button.setImage(UIImage(named: "八荒剑神"), for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 5,left: 10,bottom: 5,right: 60)
button.titleEdgeInsets = UIEdgeInsets(top: 0,left: 10,bottom: 0,right: 0)
}
弹窗
func yUIAlertController() {
let alertController = UIAlertController(title: "系统提示",message: "您确定要离开吗?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: .default, handler: {action in
print("点击了确定")})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
}
随机数-颜色随机
self.view.backgroundColor = UIColor(red:CGFloat(arc4random()%255)/255.0, green:CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0,alpha: 1)
UISegmentedControl分段控制器
func yUISegmentedControl(){
let items = ["登陆","注册","找回密码"]
let segmented = UISegmentedControl(items: items)
segmented.frame = CGRect(x: 100, y: 200, width: 200, height: 50)
segmented.selectedSegmentIndex = 0
}
UIImageView-UIImage-动画
func yUIImage(){
let imageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
var imageArray = Array<UIImage>()
for index in 0..<7{
let image = UIImage (named: "png\(index+1)")
imageArray.append(image!)
}
imageView.animationImages = imageArray
imageView.animationDuration = 4
imageView.animationRepeatCount = 1
imageView.startAnimating()
}
UISlider滑块-拖动滑块改变View的大小changeView5Value()
func yUISlider(){
var value : Float!
let slider = UISlider(frame: CGRect(x: CGFloat(arc4random_uniform(UInt32(UIScreen.main.bounds.width))+1), y: CGFloat(arc4random_uniform(UInt32(UIScreen.main.bounds.height))+1), width: 100, height: 100))
slider.isContinuous = true
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = 10
value = slider.value
slider.minimumTrackTintColor = UIColor.green
slider.maximumTrackTintColor = UIColor.red
slider.thumbTintColor = UIColor.gray
}
UIScrollview滚动视图
func yUIScrollview(){
let scrollView = UIScrollView(frame: UIScreen.main.bounds)
scrollView.backgroundColor = UIColor.orange
let imageView2 = UIImageView(frame: UIScreen.main.bounds)
imageView2.image = UIImage(named: "png")
scrollView.addSubview(imageView2)
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width * 2, height: UIScreen.main.bounds.height * 2)
scrollView.bounces = true
scrollView.alwaysBounceHorizontal = true
scrollView.alwaysBounceVertical = true
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = true
scrollView.showsVerticalScrollIndicator = true
scrollView.indicatorStyle = .black
scrollView.scrollsToTop = true
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 2.0
scrollView.bouncesZoom = true
}
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool{
print("scrollViewShouldScrollToTop")
return true
}
func scrollViewDidScrollToTop(_ scrollView: UIScrollView){
print("scrollViewDidScrollToTop")
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging")
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity:CGPoint, targetContentoffset: UnsafeMutablePointer<CGPoint>){
print("scrollViewWillEndDragging")
}
func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
print("scrollViewWillBeginDecelerating")
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
print("scrollViewDidEndDecelerating")
}
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?){
print("scrollViewWillBeginZooming")
}
func scrollViewDidZoom(_ scrollView: UIScrollView){
print("scrollViewDidZoom")
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
print("scrollViewDidEndDragging")
}
func scrollViewDidEndZoom(_ scrollView: UIScrollView, with view: UIView?, atAcale scale: CGFloat){
print("scrollViewDidEndZoom")
}
func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
print("scrollViewDidEndScrollingAnimation")
}
func scrollViewDidScroll(_ scrollView: UIScrollView){
print("scrollViewDidScroll")
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?{
print("viewForZooming")
return scrollView.subviews.first
}
UITableView
func yUITableView(){
let tableView1 = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height) , style: .plain)
tableView1.backgroundColor = UIColor.blue
tableView1.showsHorizontalScrollIndicator = true
tableView1.setEditing(true, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return UITableViewCell.EditingStyle.insert
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.insert{
tableView.insertRows(at: [indexPath], with: UITableView.RowAnimation.right)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell{
var cell = tableView.dequeueReusableCell(withIdentifier: "cellID")
if cell == nil{
cell = UITableViewCell(style: .default, reuseIdentifier: "cellID")
}
cell? .textLabel? .text = "第\(indexPath.section)分区第\(indexPath.row)行"
return cell!
}
}
let label = UILabel(frame: CGRect(x: 20, y: 20, width: self.view.bounds.size.width-40, height: 60))
label.text = "第\(indexPath.section)分区第\(indexPath.row)行"
label.font = UIFont.systemFont(ofSize: 60)
cell?.addSubview(label)
let image = UIImage(named: images[indexPath.row])
let imageView = UIImageView(image: image)
imageView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 100)
cell?.addSubview(imageView)
cell?.sendSubviewToBack(imageView)
cell? .backgroundColor = UIColor(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1)
return cell!
}
*/
func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
view.backgroundColor = UIColor.red
return view
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
view.backgroundColor = UIColor.green
return view
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let alertController = UIAlertController(title: "系统提示",message: "当前cell内容为:\(indexPath.row)", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "返回", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
tableView.deselectRow(at: indexPath, animated:true)
}
UINavigationController
func yUINavigationController(){
let nav = UINavigationController(rootViewController: yUIView())
}
UICollectionView
func yUICollectionView(){
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 100,height: 150)
layout.minimumLineSpacing = 30
layout.minimumInteritemSpacing = 10
let collectionView = UICollectionView(frame: CGRect(x: 50, y: 10, width: UIScreen.main.bounds.width-100, height: UIScreen.main.bounds.height-10) , collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.red
collectionView.register(NSClassFromString("UICollectionViewCell"), forCellWithReuseIdentifier: "cellID")
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let identifier = "reusedCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
let imageView: UIImageView? = cell.viewWithTag(1) as? UIImageView
if imageView == nil{
let image = UIImage(named: "images[indexPath.row]")
let imageView = UIImageView(image: image)
imageView.frame = CGRect(x: 0, y: 0, width: 150, height: 135)
imageView.layer.opacity = 0.5
imageView.tag = 1
cell.addSubview(imageView)
}else{
imageView? .image = UIImage(named: "images[indexPath.row]")
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
let imageView = cell? .viewWithTag(1)
imageView? .layer.opacity = 1.0
}
自定义UITabBarController
class yUITabBarController: UITabBarController {
var selectedBtn: UIButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
let rect = self.tabBar.frame;
self.tabBar.removeFromSuperview()
let myView = UIView()
myView.frame = CGRect(x: rect.minX, y: rect.maxY - rect.size.width/5, width: rect.size.width, height: rect.size.width/5)
myView.backgroundColor = UIColor.red
self.view.addSubview(myView)
for i in 0...5 {
let btn = UIButton()
let imageName = "TabBar\(i + 1)"
let imageNameSel = "TabBar\(i + 1)Sel"
btn.backgroundColor = UIColor(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1)
btn.setImage(UIImage.init(named: imageName), for: .normal)
btn.setImage(UIImage.init(named: imageNameSel), for: .selected)
let x = CGFloat(i * Int(myView.frame.size.width) / 5)
var height = myView.frame.size.height/1.5
if i == 0 || i == 4{
height = myView.frame.size.height
}
btn.frame = CGRect(x: x, y: 0, width: myView.frame.size.width / 5, height: height);
if i == 0 || i == 4{
btn.layer.cornerRadius = btn.frame.size.width/2
btn.clipsToBounds = true
}
myView.addSubview(btn)
btn.tag = i;
btn.addTarget(self, action: #selector(clickBtn), for:.touchUpInside)
if (0 == i) {
btn.isSelected = true;
selectedBtn = btn;
}
}
}
@objc func clickBtn(button: UIButton) {
selectedBtn.isSelected = false
button.isSelected = true
selectedBtn = button
}
}
view1位置跟随手指位置
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let view1 = UIView(frame: CGRect(x: 100, y: 50, width: 100, height: 100))
let touch = (touches as NSSet).anyObject() as! UITouch
let nowLocation = touch.location(in: self.view)
let preLocation = touch.previousLocation(in: self.view)
let offsetX = nowLocation.x - preLocation.x
let offsetY = nowLocation.y - preLocation.y
var center = view1.center
if(nowLocation.x < center.x+50 && nowLocation.x > center.x-50){
if(nowLocation.y < center.y+50 && nowLocation.y > center.y-50){
center.x += offsetX
center.y += offsetY
view1.center = center
}
}
}
|