gjnilsen / ypmagnifyingglass Goto Github PK
View Code? Open in Web Editor NEWMagnifying Glass for Swift
License: MIT License
Magnifying Glass for Swift
License: MIT License
Select the view you want to magnify, and embed it in another view. Set the views class to YPMagnifyingGlass.
I believe the code works if you set the view to YPMagnifyingView instead of YPMagnifyingGlass.
I have followed your instruction its working for me.
Some modifications are needed for this to work properly in Swift 2.0
`//
// YPMagnifyingView.swift
// YPMagnifyingGlass
//
// Created by Geert-Jan Nilsen on 02/06/15.
// Copyright (c) 2015 Yuppielabel.com All rights reserved.
//
import UIKit
public class YPMagnifyingView: UIView {
public var YPMagnifyingViewDefaultShowDelay: NSTimeInterval = 0.2;
private var magnifyingGlassShowDelay: NSTimeInterval
private var touchTimer: NSTimer!
public var magnifyingGlass: YPMagnifyingGlass = YPMagnifyingGlass()
override public init(frame: CGRect) {
self.magnifyingGlassShowDelay = YPMagnifyingViewDefaultShowDelay
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
self.magnifyingGlassShowDelay = YPMagnifyingViewDefaultShowDelay
super.init(coder: aDecoder)
}
// MARK: - Touch Events
override public func touchesBegan(touches: Set, withEvent event: UIEvent?) {
if let touch: UITouch = touches.first {
self.touchTimer = NSTimer.scheduledTimerWithTimeInterval(magnifyingGlassShowDelay, target: self, selector: Selector("addMagnifyingGlassTimer:"), userInfo: NSValue(CGPoint: touch.locationInView(self)), repeats: false)
}
}
override public func touchesMoved(touches: Set, withEvent event: UIEvent?) {
if let touch: UITouch = touches.first {
self.updateMagnifyingGlassAtPoint(touch.locationInView(self))
}
}
override public func touchesEnded(touches: Set, withEvent event: UIEvent?) {
self.touchTimer.invalidate()
self.touchTimer = nil
self.removeMagnifyingGlass()
}
// MARK: - Private Functions
private func addMagnifyingGlassAtPoint(point: CGPoint) {
self.magnifyingGlass.viewToMagnify = self as UIView
self.magnifyingGlass.touchPoint = point
let selfView: UIView = self as UIView
// let superView: UIView = self.superview!
selfView.addSubview(self.magnifyingGlass)
self.magnifyingGlass.setNeedsDisplay()
}
private func removeMagnifyingGlass() {
self.magnifyingGlass.removeFromSuperview()
}
private func updateMagnifyingGlassAtPoint(point: CGPoint) {
self.magnifyingGlass.touchPoint = point
self.magnifyingGlass.setNeedsDisplay()
}
public func addMagnifyingGlassTimer(timer: NSTimer) {
let value: AnyObject? = timer.userInfo
if let point = value?.CGPointValue {
self.addMagnifyingGlassAtPoint(point)
}
}
}
//
// YPMagnifyingGlass.swift
// YPMagnifyingGlass
//
// Created by Geert-Jan Nilsen on 02/06/15.
// Copyright (c) 2015 Yuppielabel.com All rights reserved.
//
import UIKit
import QuartzCore
public class YPMagnifyingGlass: UIView {
public var viewToMagnify: UIView!
public var touchPoint: CGPoint! {
didSet {
self.center = CGPointMake(touchPoint.x + touchPointOffset.x, touchPoint.y + touchPointOffset.y)
}
}
public var touchPointOffset: CGPoint!
public var scale: CGFloat!
public var scaleAtTouchPoint: Bool!
public var YPMagnifyingGlassDefaultRadius: CGFloat = 40.0
public var YPMagnifyingGlassDefaultOffset: CGFloat = -40.0
public var YPMagnifyingGlassDefaultScale: CGFloat = 2.0
public func initViewToMagnify(viewToMagnify: UIView, touchPoint: CGPoint, touchPointOffset: CGPoint, scale: CGFloat, scaleAtTouchPoint: Bool) {
self.viewToMagnify = viewToMagnify
self.touchPoint = touchPoint
self.touchPointOffset = touchPointOffset
self.scale = scale
self.scaleAtTouchPoint = scaleAtTouchPoint
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
required public override init(frame: CGRect) {
super.init(frame: frame)
self.layer.borderColor = UIColor.lightGrayColor().CGColor
self.layer.borderWidth = 3
self.layer.cornerRadius = frame.size.width / 2
self.layer.masksToBounds = true
self.touchPointOffset = CGPointMake(0, YPMagnifyingGlassDefaultOffset)
self.scale = YPMagnifyingGlassDefaultScale
self.viewToMagnify = nil
self.scaleAtTouchPoint = true
}
private func setFrame(frame: CGRect) {
super.frame = frame
self.layer.cornerRadius = frame.size.width / 2
}
public override func drawRect(rect: CGRect) {
let context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextTranslateCTM(context, self.frame.size.width/2, self.frame.size.height/2)
CGContextScaleCTM(context, self.scale, self.scale)
CGContextTranslateCTM(context, -self.touchPoint.x, -self.touchPoint.y + (self.scaleAtTouchPoint != nil ? 0 : self.bounds.size.height/2))
self.viewToMagnify.layer.renderInContext(context)
}
}
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.