How to draw line between two views in Swift - swift

I want to Draw lines from one object to another object. Like a Matching object game. I want Swift language with UIKit Use. Here I attach the Sample Screenshot.
Does anyone have experience with this? then please help me.

You have a couple of choices.
You can create a custom subclass of UIView that implements the draw(_:) function to draw custom content. That view would need to big enough to enclose all of your custom drawing. You'd fill most of the view with clear, and then draw the lines you want using Core Graphics.
The other option is to use Core Animation layers. You could add a CAShapeLayer to your view's layer, set up the shape layer with the desired line thickness and color, and add a path to the layer containing the lines you want to draw. (Note that if you use CAShapeLayers, all the lines drawn in a single shape layer will be the same color. You'll need multiple shape layers in order to draw in multiple colors.)
Both approaches will require some research. Shape layers are more efficient and take better advantage of the graphics hardware on iOS devices, but using them has a fairly steep learning curve.
You should be able to google examples of both approaches. Try search phrases like "Custom drawing in a UIView" and "drawing with CAShapeLayer". I wrote a little demo app called OvalView that demonstrates how to create a UIView subclass that manages a shape layer.
Edit:
I adapted the code from my sample app into a demo called LinesBetweenViews. The new demo has a custom UIView that draws lines between any pairs of subviews you put into it.
Here is a screenshot of the demo:
If you answer my questions I can upload the demo app to Github so you can look at it.
Edit #2
See this Github repo for a sample project that draws lines between pairs of subviews.

Related

How identify shapes drawn with Core Graphics in Swift?

I'm creating a project in Swift that draws some shapes, based on this tutorial. My project involves some things to most, like removing the shape drawn on the View.
To begin need to recognize them in layers, or uniquely identify what form should remove.
I'm working on it just over two weeks, but I'm running out of idea what to do.
My question:
How can I identify and separate shapes drawn with Core Graphics in Swift?
I found the way:
for layer in self.view.layer.sublayers! {
print(layer)
}
I can get all layers drawing in my view.

Drawing graphs on iOS: Use layers or override drawRect?

Reading through the documentation, it's not clear to me if overriding a UIView's drawRect or using layers is appropriate.
I am going to be rendering two kinds of graphs. A line graph and a single-bar bar graph. Both will have ticks along the axis and text aligned with the ticks. Below I only show four ticks per axis, but there could be more.
What is the better way of drawing the ticks? Should I use an individual layer for each tick, or render them all at once on a separate view using drawRect?
Is there another way to render the text other than using a separate UILabel for each?
For the bar graph, I am using a CAGradientLayer for the bar. For the line graph, is it even possible to render this using layers?
Sample Graph
In the end, I used a combination of overriding drawRect and CALayer/CAShapeLayer. Having now been through the process of implementing it all from scratch, I would go with shapes and layers.
The shapes and layers approach is plenty performant, and is a more robust solution. In the end it takes less code if you are doing things like animation.

How to draw a circle by following MVC architecture

I am new to iOS platform and i heard about MVC archtecture.
To draw a circle i just create a separate UIView class and override the drawRect: and able to do this.
But now i want to rebuild the same project using MVC architecture.The main aim is to separate my Model part from View & Controller part.So that i can extend my project.
So how can i do this?
Any sample application for reference?
I'm not sure what you want to separate. Drawing a circle would generally fit into the "View" part of the MVC architecture, which is what you have already done. Code that would, for instance, change the colour of the circle would live in the "Controller" part, which on iOS is a UIViewController. If you had something storing information on what the circle looked like (i.e. size, colour, etc.), that could be considered part of the model, and can be stored in its own class, pulled in by the view controller and passed to the view when it is neede.
Here is a link that may help.
http://www.bit-101.com/blog/?p=1969
As for the model side of things, Core Data is a technology that can help.
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html#//apple_ref/doc/uid/TP40001075
The separation would be the drawing code in drawRect and the size and location in a model with a controller getting the circle attributes from the model and requesting the drawing and passing the attributes to the view. The model might be another class or an API supplied class such as an NSDictionary.
By creating a separate model if there were multiple circles the controller could make multiple draw requests, one per model circle. Or there could be multiple views that the circle(s) would be drawn in or different representations such as a text list of the circles attributed in one view and graphic circles in another.
Many patterns seem trivial and not particularly useful in the trivial case yet in a real-world program work very well.

Can you animate gradients using Quartz in an iPhone?

I am new to iPhone development and am currently toying with recreating a charting tool I developed for Silverlight.
Currently I'm using a gradient to 'fill' a rectangle representing a bar within a chart. Is it possible to animate this gradient so it changes colour when a user touches the bar within the chart.
I have looked through the Core Animation guides provided by Apple but cannot see a property which targets gradients. I suppose I could use a transition to fade between two rects, one of which has my starting gradient and the second with the 'touched' version but this would mean obviously drawing multiple rect objects for each bar with I assume extra performace overheads.
Any ideas?
Yes, indeed you can animated gradients with Core Animation.
The CAGradientLayer class that came out in 3.0 has a nice API for rendering gradients into a layer and animating color and color-stop changes as well.
I did a post on this class a little while back, along with some sample code that's linked at the bottom.
In the sample I animate the gradient by building a CABasicAnimation, but you can implicitly animate the change as well, by just passing a new array of colors to the gradient layer's colors property. Use implicit animations unless you have a reason not to.
Check that out and let me know if you have any questions specific to the UI you're trying to animate.

How do I use CALayer with the iPhone?

Currently, I have a UIView subclass that "stamps" a single 2px by 2px CGLayerRef across the screen, up to 160 x 240 times.
I currently animate this by moving the UIView "up" the screen 2 pixels (actually, a UIImageView) and then drawing the next "row".
Would using multiple CALayer layers speed up performance of rendering this animation?
Are there tutorials, sample applications or code snippets for use of CALayer with the iPhone SDK?
The reason I ask is that most of the code snippets I find that demonstrate simple examples of CALayer employ method calls that do not work with the iPhone SDK. I appreciate any advice or pointers.
Okay, well, if you want something that has some good examples of CA good that draws things like that and works on the phone, I recommend the GeekGameBoard code that Jens Aflke published (it is an improved version of some Apple demo code).
Based on what you are describing I think you are doing somthing way more complicated than it needs be. My impression is you want basically a static view that you are animating by shifting its position so that it is partially off screen. If you just need to set some static content in your drawRect going through layers is not going to be faster than just calling CGFillRect() with your color. After that you could just use implicit animations and the animator proxy on UIView to move the view. I suspect you could even get rid of the custom drawRect: implementation with a patterned UIColor, but I honestly have not benchmarked the difference between the two.
What CALayer methods are you seeing that don't work on iPhone? Aside from animation features tied to CoreImage I have not noticed much that is missing. The big thing you are likely to notice is that all views are layer backed (so you do not need to do anything special to use layers, you can just grab a UIView's layer through the layer accessors methos), and the coordinate system has a top left origin.
In any event, generally having more things is slower than having fewer things. If you are just repeating the same pattern over and over again you are likely to find the best performance is implementing a custom UIView/CALayer/UIColor that knows how to draw what you want, rather than placing visually identical layers or views next to each other.
Having said that, generally layers are lighter weight than views, so if you have a lot of separate elements that you need to keep logically separated you will find that moving to layers can be a win over using views.
You might want to look at -[UIColor initWithPatternImage:] depending on exactly what you are trying to do. If you are using this two pixel pattern as a background color you could just make a UIColor that draws it and set the background.
What CALayer methods are you seeing that don't work on iPhone?
As one example, I tried implementing the grid demo here, without much luck. It looks like CAConstraintLayoutManager and CAConstraint are not available in QuartzCore.h.
In another attempt, I tried a very simple, small 20x20 CALayer object as a sublayer of my UIView's layer property, but that didn't show up.
Right now, I have a custom UIView of which I override the drawRect method. In drawRect I grab a context and render two types of CGLayerRefs:
At "off" cells I draw the background color across the entire 320x480 canvas.
At "on" cells, I either draw a single CGLayerRef across a grid of 320x480 pixels (initialization) or across a 320x2 row (animation).
During animation, I make a UIImageView clip view from 320x478 pixels, and draw a single row. This "pushes" my bitmap up the screen two pixels at a time.
Basically, I'd like to test whether or not using CALayer will accomplish two things:
Make my rendering faster, if CALayer has less overhead than what I'm doing now
Make my animation smoother, by letting me transition a layer up the screen smoothly
Unfortunately, I can't seem to get a basic CALayer working at the moment, and haven't found a good chunk of sample code to look at and play with.