Category: swift

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. First released in 2014, Swift was developed as a replacement for Apple’s earlier programming language Objective-C, as Objective-C had been largely unchanged since the early 1980s and lacked modern language features.

  • SwiftUI combine rectangle and triangle in shape

    SwiftUI combine rectangle and triangle in shape

    SwiftUI combine rectangle and triangle in shape

    Making custom shape is not hard in SwiftUI by implementing func path(in rect: CGRect) -> Path. You just have to draw points in your desired location in 2D coordinate space by path.move() and path.addLine() func, a line will be drawn automatically.

    struct MomentumArrow: Shape {
        
        func path(in rect: CGRect) -> Path {
            var path = Path()
            
            let squareHeight = rect.height * 0.65
            
            // Rect
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: 0, y: squareHeight))
            path.addLine(to: CGPoint(x: rect.width, y: squareHeight))
            path.addLine(to: CGPoint(x: rect.width, y: 0))
            path.addLine(to: CGPoint(x: 0, y: 0))
            
            // Triangle
            path.move(to: CGPoint(x: 0, y: squareHeight))
            path.addLine(to: CGPoint(x: rect.midX, y: rect.height))
            path.addLine(to: CGPoint(x: rect.width, y: squareHeight))
        
            return path
        }
        
    }
    Spread the love
  • Capture first frame of the video as an image on iOS and macOS in swift

    Capture first frame of the video as an image on iOS and macOS in swift

    import Foundation
    import Cocoa
    import AVKit
    import AVFoundation
    
    let asset: AVURLAsset = AVURLAsset.init(url: "URL OF ANY VIDEO local/remote")
    let imgGenerator: AVAssetImageGenerator = AVAssetImageGenerator.init(asset: asset)
    imgGenerator.appliesPreferredTrackTransform = true
                
    imgGenerator.generateCGImagesAsynchronously(forTimes: [NSValue(time: CMTime.zero)]) { (_, image, _, res, error) in
                    
                    if error == nil {
                        DispatchQueue.main.async {
                               let imageView = NSImage.init(cgImage: image!, size: NSSize.init(width: 180.0, height: 180.0));
                        }
                    } else {
                        DispatchQueue.main.async {
                            // Some other fallback image
                        }
                    }
    }
    
    Spread the love
  • NSTableView custom highlight color with NSTableRowView

    Getting the right color when you select NSTableView’s row was frustrating for me since I didn’t know how to do it properly. But it is really simple, all you need to do is having a class inherit from NSTableRowView and override its method drawSelection(in dirtyRect: NSRect).

    import Foundation
    import Cocoa
    
    class MenuTableRowView: NSTableRowView {
    
        override func drawSelection(in dirtyRect: NSRect) {
            if self.selectionHighlightStyle != .none {
                let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5)
                
                if let color = NSColor.init(named: NSColor.Name("menu_table_selection_color")) {
                    color.setFill()
                }
                
                let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 0, yRadius: 0)
                selectionPath.fill()
            }
        }
        
    }
    

    Change menu_table_selection_color value in Assets.xcassets file.

    Custom NSTableRowView

    Now go to your NSTableViewDelegate’s method add your custom NSTableRowView.

     func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
            return MenuTableRowView()
    }

    Here you can see how my Storyboard looks like. Table view highlight value must be regular.

    Spread the love