MMSLayeredView 2.1.0

MMSLayeredView 2.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2017
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by William Miller.




  • By
  • William Miller

MMSLayeredView

CI Status
Version
License
Platform
Readme Score

This MMSLayeredView class provides the features for layering UIViews on a background view and saving the composite image to a png file. The layered views can be moved, resized, and scaled within the confines of the view's frame.

Example

## Basic Usage To run the example project, clone the repo, and run `pod install` from the Example directory first.

Create a MMSLayeredView object. Add it to a subview of one of the application's UIViewController subclasses. Or in Interface Builder add a UIView to a subview of one of the UIViewControllers. In the Identity Inspector, select the class MMSLayeredView. Size it as you see fit for your application's needs.

Add a background view to the object. The background view can be a UIView or any subclass of UIView. Typically, you would add a UIImageView as the background view. Call the method addBackgroundView to set the background view.

override func viewDidLoad() {
    super.viewDidLoad()

    let exampleLayeredView = MMSLayeredView.init(frame: CGRectMake(20.0, 20.0, 100.0, 100.0))

    view.addSubview(exampleLayeredView)

    let backgroundImage = UIImage.init(named: "example.jpg")

    let backgroundView = UIImageView(image: backgroundImage)

    backgroundView.contentMode = .ScaleToFill

    sourceView.addBackgroundView(backgroundView)

}

addBackgroundImage adds the view to the subview, resizes the height and width to match the dimensions of exampleLayeredView and sets the frame's origin to (0,0).

Once the background view is set, all subviews added will display above the background view in the z-order. The background view can be set in a sequence with respect to adding subviews; it will always display behind all the subviews in the z-order.

Set a unique tag for the view to access any one of the subviews with the method:

    viewWithTag(tag:Int)

Add, remove, and reorder subviews to the layered view with UIView view hiearchy methods:

    addSubview(view:UIView)

    removeFromSuperview()

    insertSubview(view:UIView, atIndex: Int)

    insertSubview(view: UIView, aboveSubview: UIView)

    insertSubview(view: UIView, belowSubview: UIView)

    exchangeSubviewAtIndex(index1: Int, withSubviewAtIndex: Int)

    sendSubviewToBack(view: UIView)

    bringSubviewToFront(view: UIView)

The user selects one of the subviews for the focus of application operations by tapping the view. When tapped the view shows it has focus by filling the area outside it's perimeter with a translucent black. To get the UIView with focus access the property:

public var viewWithFocus: UIView?

Finally, this class supports the capability for merging the background view and subviews into image. With this feature, you can use the class to build applications providing the feature to layer text over images, decorate images with stickers, or both. This is the public method supporting export to image:

public func mergeTextAndImage() -> UIImage?

User Interaction

This class gives the application user the following operations:

  • Scale Subview: Pinch gesture.
  • Move Subview: Double tap to give move focus to the view then drag it.
  • Size Subview: One finger drag gesture to size dimensions independently.
  • Give Subview Focus: One finger tap gesture gives focus to a subview and removes it when it already has focus. Tap the background view to give it focus.

Requirements

  • MMSLayeredView requires iOS 8.3 or later.
  • Swift 3.0

Installation

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

pod "MMSLayeredView"

Author

William Miller, [email protected]

License

This project is is available under the MIT license. See the LICENSE file for more info. Add attibution by linking to the project page is appreciated.