Zooming in an NSView - objective-c

I have an NSView in which the user can draw circles. These circles are stored as an array of NSBezierPaths, and in drawRect:, I loop through the array and call -stroke on each of the paths. How do I add a button to zoom in and out the NSView? Just change the bounds of the view?
Thanks.

Send your view a scaleUnitSquareToSize: message.

You might also find this informative:
https://developer.apple.com/library/content/qa/qa1346/_index.html
The code in that document lets you add a "scale" property to a view.

The above answers didn't work for my scenario but led me to a solution.
The updated link to #Peter's answer was helpful: scaleUnitSquareToSize
I have found two soultions for zooming:
Cropping the bounds manually
Scalling the bounds with scaleUnitSquareToSize
I have created a small test project. Both solutions can be found on my GitHub repo : BoundsAndFramesCroppingAndScalling
To understand bounds vs frames read this SO article: difference-between-the-frame-and-the-bounds.
Swift scalling code:
let scaleSize = NSSize(width: 0.5, height: 0.5)
// 0.5 - Half the size
// 1.0 - No calling
// 2.0 - double the size , ... etc
myView?.scaleUnitSquare(to: scaleSize)
myView?.needsDisplay = true

Related

Is it possible to use iOS Charts (Daniel Gindi) to generate graphs in pdf document on iOS

I am using iOS Charts (Daniel Gindi) to generate graphs in an iOS app and I want to be able to generate a PDF report with those graphs included in the body of the report. Can anyone explain how to go about doing this. Ideally I don't want to generate an image from the UIView that shows in the app because the size/resolution would not be suitable for the PDF document.
As I understand it there are a few options:
use the graphics context for the pdf document to draw the graph on - it's not clear whether this would be possible when using the Charts library
use a UIView somehow to generate the graph and generate a PDF image data from that, embed this image into the pdf report
It seems like option 1 is probably the preferred way to get best resolution/control - somewhat speculative - doing it this way means you should be able to specify the exact position and size and get the correct font sizes, line thicknesses, etc..
Using option 2 means you have to figure out the scaling between a UIView and the PDF page view and I am not sure how these would map to each other.
Can anyone provide any suggestions on the following:
Is it possible to use Charts to generate graphs in a PDF document, and if so how?
If not what other options are there, short of writing custom drawing code.
OK so here is what I have done
Option 1: Using a UIView.layer to render on the PDF CGContect
func drawLineGraph(x: CGFloat, y: CGFloat)->CGRect{
let width = (pageSize.width - 2*kBorderInset - 2*kMarginInset)/2.0 - 50.0
let renderingRect = CGRect(x: x, y: y + 50.0, width: width, height: 150.0)
// Create a view for the Graph
let graphController = LineChartController(rect: renderingRect, building: self.building)
if let currentContext = UIGraphicsGetCurrentContext() {
let frame = graphController.chartView.frame
currentContext.saveGState()
currentContext.translateBy(x:frame.origin.x, y:frame.origin.y);
graphController.chartView.layer.render(in: currentContext)
currentContext.restoreGState()
}
return renderingRect
}
The graphController is just an object that has essentially the same function as the usual parent ViewController that would contain the graph. Sets the graph parameters and data.
Once that has been done the function below is called to render on the PDF page context.
A bit of translation required to put the graphs in the correct position.
Option 2: Drawing on the PDF Page CGContect
And the solution is...ta da...
func drawBarGraph(x: CGFloat, y: CGFloat)->CGRect{
let width = (pageSize.width - 2*kBorderInset - 2*kMarginInset)/2.0 - 50.0
let renderingRect = CGRect(x: x + width + 50, y: y + 50.0, width: width, height: 150.0)
// Create a view for the Graph
let graphController = BarChartController(rect: renderingRect, building: self.building)
if let currentContext = UIGraphicsGetCurrentContext() {
let frame = graphController.chartView.frame
currentContext.saveGState()
currentContext.translateBy(x:frame.origin.x, y:frame.origin.y)
//graphController.chartView.layer.render(in: currentContext)
graphController.chartView.draw(frame)
currentContext.restoreGState()
}
return renderingRect
}
Since the current context is set to the PDF page's context just call the charts draw() function directly passing the frame rectangle.
What have I missed here, can it be this easy ?
You can find a copy of the generated PDF here as well as sample code.

CreateJs Drawing with alpha

I implemented a little drawing function into my app with CreateJS like so:
var currentPosition = this.posOnStage(event);
var drawing = container.getChildByName('drawing');
drawing.graphics.ss(this.brushSize, "round").s(this.brushColor);
drawing.graphics.mt(this._lastMousePosition.x, this._lastMousePosition.y);
drawing.graphics.lt(currentPosition.x, currentPosition.y);
drawing.alpha = this.brushAlpha;
container.updateCache(this.enableErasing ? "destination-out" : "source-over");
drawing.graphics.clear();
this._lastMousePosition = this.posOnStage(event);
As you can see, the alpha value of this drawing can change. Sadly you can draw over a point you once did draw, so when you draw over a point multiple times the alpha effect will go away. Any idea how to solve this ?
Thanks :)
EDIT:
I tried it like gskinner and Lanny 7 proposed, but it didn't work. I attached a image so you can see the problem.
As suggested by Lanny, apply the alpha to the actual stroke, not to the Shape. You can use Graphics methods to help with this.
For example:
// set the brush color to red with the current brush alpha:
this.brushColor = createjs.Graphics.getRGB(255, 0, 0, this.brushAlpha);

Animating UIVisualEffectView Blur Radius?

As the title says it, is there a way to animate a UIVisualEffectView's blur radius? I have a dynamic background behind the view so the ImageEffects addition can't be used... The only thing that can do this as far as I know is to animate the opacity but iOS complains saying that doing that breaks the EffectView so it definitely seems like a bad idea... Any help would be gladly appreciated.
The answer is yes. Here's an example for animating from no blur -> blur:
// When creating your view...
let blurView = UIVisualEffectView()
// Later, when you want to animate...
UIView.animateWithDuration(1.0) { () -> Void in
blurView.effect = UIBlurEffect(style: .Dark)
}
This will animate the blur radius from zero (totally transparent, or rather - no blur effect at all) to the default radius (fully blurred) over the duration of one second. And to do the reverse animation:
UIView.animateWithDuration(1.0) { () -> Void in
blurView.effect = nil
}
The resulting animations transform the blur radius smoothly, even though you're actually adding/removing the blur effect entirely - UIKit just knows what to do behind the scenes.
Note that this wasn't always possible: Until recently (not sure when), a UIVisualEffectView had to be initialized with a UIVisualEffect, and the effect property was read-only. Now, effect is both optional and read/write (though the documentation isn't updated...), and UIVisualEffectView includes an empty initializer, enabling us to perform these animations.
The only restriction is that you cannot manually assign a custom blur radius to a UIVisualEffectView - you can only animate between 'no blur' and 'fully blurred'.
EDIT: In case anybody is interested, I've created a subclass of UIVisualEffectView that gives you full control over blur radius. The caveat is that it uses a private UIKit API, so you probably shouldn't submit apps for review using it. However, it's still interesting and useful for prototypes or internal applications:
https://github.com/collinhundley/APCustomBlurView

Is 'NSIntersectsRect' from a non-public API? If so, are there alternatives?

I'm using the above class for an app that I am trying to submit to the app store. It gives me the error "the app references non-public symbols in Payload/app name.app/app name: NSIntersectsRect". If this is non-public, does anyone know of a class that can serve the same function (that is, detect when a rectangle overlaps another), or am I going to have to figure out a custom workaround?
Sounds like a mistake… anyways - What about CGRectIntersectsRect?
According to docs for NSIntersectionRect
If the two rectangles don’t overlap, the returned rectangle has its origin at (0.0, 0.0) and zero width and height (including situations where the intersection is a point or a line segment)
So my suggestion is create a method like
- (BOOL)rect:(NSRect)rect1 intersectsRect:(NSRect)rect2 {
NSRect testRect = NSIntersectionRect(rect1, rect2);
return !NSEqualRects(testRect, NSMakeRect(0.0, 0.0, 0.0, 0.0)
}

GraphKit framework in Cocoa

I try to draw an XY graph using GraphKit.
Information of this framework is very limited on the internet...
Here's what I did:
// a xychart is predefined in header as GRChart
GRDateSet *dataset = [[GRXYDataSet alloc] initWithOwnerChart:xychart];
[xychart addDataSet:dataSet loadData:YES];
[xychart reloaddata];
also I implement delegate methods:
(double)chart:(GRChartView *)aChart xValueForDataSet:(GFDataSet*)aDataSet element:(NSUInteger)index
{ return index * 10.0; }
(double)chart:(GRChartView *)aChart yValueForDataSet:(GFDataSet*)aDataSet element:(NSUInteger)index
{ return index * 10.0; }
(NSUInteger) chart:(GRChartView *)aChart numberOfElementsForDataSet:(GFDataSet*)aDataSet {
return 10;
}
however, it only draws the axes but no data points at all...
what did I miss here?
thanks!
I got it. This framework only stores data points and draws axes according to the data points. (It automatically calculates the bounds of each axes and zoom into a suitable plot area.)
However, no drawing method is rooted. To get an immediate graph, I have to use GRAreaDataSet, which is a subclass of GRXYDataSet. Then it will draw an area chart.
I also tried out core-plot. But it's more difficult to use to me. I have to calculate the bounds myself; and padding the graph to show the label values of axes. Also, it's not so beautiful if I don't customize the symbols and lines. However, the default GraphKit charting is nice-looking enough. Though it doesn't have a document...
I'll try to write a tutorial of it when I try out everything in it :)