All Projects → lm2343635 → Shapeview

lm2343635 / Shapeview

Licence: mit
A customized shape view with shadow and transparent background supported.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Shapeview

Shadowview
An iOS Library that makes shadows management easy on UIView.
Stars: ✭ 391 (+334.44%)
Mutual labels:  uiview, shadow
Afviewshaker
Stars: ✭ 1,214 (+1248.89%)
Mutual labels:  uiview
Rwidgethelper
Android UI 快速开发,专治原生控件各种不服
Stars: ✭ 996 (+1006.67%)
Mutual labels:  shadow
Unity2019shaderdevelopment
Creating Shaders (via code and Shader Graph) in Unity 2019
Stars: ✭ 65 (-27.78%)
Mutual labels:  mask
Brazilian Utils
Utils library for specific Brazilian businesses
Stars: ✭ 1,023 (+1036.67%)
Mutual labels:  mask
Capsuleview
An easy to use drop-in view to create a beautiful card style effect with a title placeholder and body content.
Stars: ✭ 68 (-24.44%)
Mutual labels:  uiview
Xmnetworking
A lightweight but powerful network library with simplified and expressive syntax based on AFNetworking.
Stars: ✭ 980 (+988.89%)
Mutual labels:  ios-lib
Ios Arkit
iOS Location based Augmented Reality engine
Stars: ✭ 88 (-2.22%)
Mutual labels:  ios-lib
Phfcomposebarview
Compose bar from iOS 7 Messages.app
Stars: ✭ 1,197 (+1230%)
Mutual labels:  uiview
Highlightr
iOS & OSX Syntax Highlighter.
Stars: ✭ 1,116 (+1140%)
Mutual labels:  ios-lib
Camerabackground
Show camera layer as a background to any UIView
Stars: ✭ 57 (-36.67%)
Mutual labels:  uiview
Real Shadow
Module that casts photorealistic shadows
Stars: ✭ 1,066 (+1084.44%)
Mutual labels:  shadow
Css Vars Ponyfill
Client-side support for CSS custom properties (aka "CSS variables") in legacy and modern browsers
Stars: ✭ 1,166 (+1195.56%)
Mutual labels:  shadow
Uiutil
UIUtil for Android, Lyrics, Tick animations, Comparisons, Satellite menus, Praise, Slide buttons, TAB indicators, Contact sorting, Drag sorting, Skidding deletes, Shadow effects, RecyclerView nesting RecyclerView, Map list Poi/Drawer effects, Progress settings, Clock set, Damping, Progress, Album, Snap, Progress, CircleDownload, AdvertSwitcher, Carousel ad, FlowLayout, Tag...; 歌词控件、打勾动画、对比、卫星菜单、点赞、滑动按钮、TAB指示器、联系人排序、拖曳排序、侧滑删除、阴影效果.、RecyclerView嵌套RecyclerView.、地图列表Poi/抽屉效果、进度设置、时钟设置、滑动阻尼、相册媒体快照、圆形下载进度,轮播广告, 流式布局,标签...
Stars: ✭ 1,018 (+1031.11%)
Mutual labels:  shadow
Bufferutil
WebSocket buffer utils
Stars: ✭ 83 (-7.78%)
Mutual labels:  mask
Cocoaasyncsocket demo
基于AsyncSocket搭建即时通讯体系 . 包含TCP连接 , 消息发送 , 消息接收 , 心跳处理 ,断网重连 , 消息超时 , 消息分发 , 数据库结构设计 , 消息丢失等 . 以及UI设计, 文本表情消息/语音消息/图片消息/视频消息/文件消息/撤回消息/提示语消息的实现思路讲解
Stars: ✭ 981 (+990%)
Mutual labels:  ios-lib
Svelte Input Mask
Input masking component for Svelte with simple API and rich customization
Stars: ✭ 56 (-37.78%)
Mutual labels:  mask
Tweenkit
Animation library for iOS in Swift
Stars: ✭ 1,146 (+1173.33%)
Mutual labels:  uiview
Ishadow
Creating a beautiful iOS style shadow [Sketch Plugin]
Stars: ✭ 89 (-1.11%)
Mutual labels:  shadow
Maska
Simple zero-dependency input mask for Vue.js and vanilla JS.
Stars: ✭ 85 (-5.56%)
Mutual labels:  mask

ShapeView Build Status Version License Platform

ShapeView support to create a view with the customized shape, shadow and transparent background at the same time.

Installation

ShapeView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'ShapeView'

Using ShapeView

ShapeView supports the following attributes.

  • path: ShapePath?
  • outerShadow: ShapeShadow
  • innerShadow: ShapeShadow
  • effect: UIVisualEffect?
  • effectAlpha: CGFloat
  • backgroundColor: UIColor?

To create a customized shape, use .custom to draw the shape as the following.

view.path = .custom { [unowned view] in
    let labelHeight = view.frame.height - Const.height
    let radius = labelHeight / 2

    $0.move(to: CGPoint(x: radius, y: 0))
    $0.addArc(
        withCenter: CGPoint(x: view.frame.width - radius, y: radius), 
        radius: radius, 
        startAngle: -.pi / 2, 
        endAngle: .pi / 2, 
        clockwise: true
    )
    $0.addLine(to: CGPoint(x: Const.left + Const.height, y: labelHeight))
    $0.addLine(to: CGPoint(x: Const.left + Const.height / 2, y: view.frame.height))
    $0.addLine(to: CGPoint(x: Const.left, y: labelHeight))
    $0.addArc(
        withCenter: CGPoint(x: radius, y: radius), 
        radius: radius, 
        startAngle: .pi / 2, 
        endAngle: -.pi / 2, 
        clockwise: true
    )
}

Using prepared shapes

In the demo app, a dialog view is created with the code above.

Demo

The following shapes are prepared.

corner(radius: CGFloat, bounds: @escaping GetBounds)
dialog(radius: CGFloat, arrowPosition: DialogArrowPosition, bounds: @escaping GetBounds)
cuteDialog(radius: CGFloat, arrowPosition: CuteDialogArrowPosition, bounds: @escaping GetBounds)
star(vertex: Int, extrusion: CGFloat = 10, bounds: GetBounds)

Here is a demo to create a dialog view.

view.path = .dialog(radius: 10, arrowPosition: .right(center: 50, width: 40, height: 20)) { [unowned self] in
    return self.bounds
}
view.outerShadow = ShapeShadow(
    radius: 10,
    color: .green,
    opacity: 1,
    offset: .zero
)

Multiple shapes

ShapeView supports to add multiple shapes with .multiple() as the following.

view.path = .multiple(
    .hollowCorner(radius: 12, outlineWidth: 2) { [unowned view] in
        view.bounds
    },
    .star(vertex: 5, extrusion: 20) { [unowned view] in
        view.bounds
    },
    .custom { [unowned view] in
        // Add a custom shape here.    
    }
)

Run the demp application to find more.

Using ShapeLayer

We provide ShapeLayer for developers to apply it to your customized view directly.

  • layerPath: ShapePath?
  • var outerShadow: ShapeShadow?
  • var innerShadow: ShapeShadow?
  • effect: UIVisualEffect?
  • effectAlpha: CGFloat
  • var didUpdateLayer: ((CAShapeLayer) -> Void)?
  • var backgroundColor: CGColor?

When the shape layer finish drawing the layer, it calls the didUpdateLayer cloure to notify the parent view. Developers can update the parent view with the first parameter in this closure.

About the Implementation

This part introduces how we implement the ShapeView, skip it if you are not interested.

Necessity to implement by ourselves

It is hard to create a customized shape with shadow and transparent background for UIView using the SDK provided by Apple. We have tried to and shadow into the customized shape layer directly with the following code.

let shapeLayer = CAShapeLayer()
shapeLayer.path = shapePath.cgPath
shapeLayer.shadow = UIColor.green.cgColor
shapeLayer.shadowRadius = 10
shapeLayer.shadowOffset = .zero
shapeLayer.shadowOpacity = 1

ShapeShadow(
    radius: 10,
    color: .green,
    opacity: 1,
    offset: .zero
)
layer.masksToBounds = true
  • Using a mask
layer.mask = shapeLayer

Mask

  • Adding a sublayer
layer.addSublayer(shapeLayer)

Sublayer

Using a mask or adding a sublayer cannot implement the effect shown in our demo screenshoot.

Structure

Shape layers

In the ShapeLayer, we add a shadow layer for the shape and effect, and a container view for storing subviews.. If the developer add subview to the ShapeView by the method addSubview(_ view:), we move it to the container view.

Creating a hollow mask layer

To solve the problems, we need to creare a hollow mask layer by ourselves. Firstly, we create a shadow layer, and insert it to the shapeLayerView.

let shadowLayer = CAShapeLayer()
shadowLayer.path = shapePath.cgPath
if shadowRadius > 0 && shadowColor != .clear {
    shadowLayer.shadowRadius = shadowRadius
    shadowLayer.shadowColor = shadowColor.cgColor
    shadowLayer.shadowOpacity = shadowOpacity
    shadowLayer.shadowOffset = shaowOffset
    shadowLayer.fillColor = shadowColor.cgColor
}
shadowLayerView.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
shadowLayerView.layer.insertSublayer(shadowLayer, at: 0)

shapelayer

The shadow layer created by CAShapeLayer is a solid layer. We need to make a cut layer shown as the red area in the following screenshoot, as a mask layer to create a hollow mask layer.

let cutLayer = CAShapeLayer()
cutLayer.path = { () -> UIBezierPath in
    let path = UIBezierPath()
    path.append(shapePath)
    path.append(screenPath)
    path.usesEvenOddFillRule = true
    return path
}().cgPath
cutLayer.fillRule = .evenOdd

cutlayer

The range of the cut layer is outside of the shape's border and inside of the screen's border. After creating the cut layer, we set it as the mask of the shadow layer view.

shadowLayerView.layer.mask = cutLayer

By setting the cut layer for the shadow layer view, we get a hollow shape view with a shadow as the following picture.

hollow-shapelayer

Next, we create a container view above the shadow view, and use the same shape path to create a shape layer as the mask of this container view.

let shapeLayer = CAShapeLayer()
shapeLayer.path = shapePath.cgPath
containerView.layer.mask = shapeLayer

The introduction above shows how to create a outer shadow, the method to create a inner shadow is same as the outer shadow. At last, we get a customized shape view with the transparent background and shadow as shown in the demo screenshot.

Author

lm2343635, [email protected]

License

ShapeView is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].