How to get a portion of a UIImageView around a point - ios

Is it possible to get a portion/area of an UIImageView around a point, either a circular portion of a given radius or quadratic?
While my users are moving a pin on a UIImageView I would like they could see a zoomed area around that point into another UIImageView.
How can I get that portion?

Consider using this project (Swift), or this one (Objective-C).
UPDATE.
I just update code from first link to be compatible with Swift 2 and add some features like #IBDesignable.
So, to get magnifier in your project you need few steps:
Download MagnifyingGlassView and MagnifierContainerView from here and add them to your project.
In Storyboard open your controller and select UIImageView which you want to magnify.
Select Editor->Embed in->View.
Select created view and assign class MagnifierContainerView. Make outlet from this view to your controller.
In viewDidLoad add this code:
magnifierView.magnifyingGlass = MagnifyingGlassView()
Where magnifierView is outlet to view you created in step 3.
Colour, width or size you can specify through Attributes inspector for MagnifierContainerView in InterfaceBuilder.
Thats all. Complete example you can find here. Hope it'll help.

Related

How can I bring a view in front of another view, in Swift?

Below are how my views are organized in IB, top->bottom when the app is started.
The user can do something to make "Category Table View Header" temporarily expand over "Name View" - however once doing so, the .TouchDown action assigned to "Category Table View Header" no longer works wherever it overlaps with "Name View" (i.e., the user can tap the header anywhere it doesn't overlap with name view and it still works).
I know that may be confusing, so I drew out some boxes. On the left is the original, right is after user action - problem is on the right the action on the red box only works if the user taps the bottom half, not the top half.
My guess is its because the header is lower in the view hierarchy than the name view, but it would be hard for me to change that without messing around with a bunch of constraints.
I also tried setting nameView.hidden = true, but that doesn't work.
If you want to bring a subview to the front, you can use:
SWIFT 4 + UPDATE
self.view.bringSubviewToFront(yourView)
SWIFT 3 UPDATE
self.view.bringSubview(toFront: yourView)
Send view to back:-
SWIFT 4+ UPDATE
self.view.sendSubviewToBack(yourView)
SWIFT 3 UPDATE
self.view.sendSubview(toBack: yourView)
SWIFT 4+ UPDATE - INSERT VIEW ON SPECIFIC LOCATION IN THE STACK
parentView.insertSubview(yourView, belowSubview: requiredViewOnStack)
parentView.insertSubview(yourView, aboveSubview: requiredViewOnStack)
Swift 5.1+
UIKit draws views back to front, which means that views higher up the stack are drawn on top of those lower down. If you want to bring a subview to the front, there's a method just for you: bringSubviewToFront(). Here's an example:
parentView.bringSubviewToFront(childView)
This method can also be used to bring any subview to the front, even if you're not sure where it is:
childView.superview?.bringSubviewToFront(childView)
In Swift 5
To bring a subview to front
self.view.bringSubviewToFront(yourView)
To send a subview to back
self.view.sendSubviewToBack(yourView)
You can take a control over the order of subviews using methods: bringSubviewToFront and sendSubviewToBack from the superview.
You can access all the subviews contained by superview using self.view.subview array.
The method has been updated since swift 3
What has worked for me and hopefully for you is using :
YourView.bringSubview(toFront: yourelementA)
YourView.bringSubview(toFront: yourelementB)
Alternatively you could use the following to send the object that is in the front:
YourView.sendSubview(toBack: yourelementC)
I hope this helps
Swift 5.4
views in UIKit are drawn from the back towards the front (like adding icing onto a cake). This means that the new views will be placed on top/over the previously placed views. (e.g. Icing is the new view and the base of the cake being the old view.
You can use the following to manipulate the views Z-position using,
Forward
parent.bringSubviewToFront(child)
Backward
parent.sendSubviewToBack(child)
bringSubViewToFront did not work in my case, Take a look
As I wanted this notification count comes before image, bringSubViewToFront did not work, So I just drag this label below to stack view of my imgae and bottom label, it worked for me.
I hope this will work for all

PaintCode - update view and pass variables

Does anyone know of a comprehensive tutorial on implementation of PaintCode, using variables with it, and getting the view to update. I am struggling my way through it. I have built a custom class, and linked a view controller, with a UIView, and I can see the simple graph I made. I have linked it up so that it is #IBDesignable & #IBInspectable and both work fine. I am just not sure how to pass a variable through to the UIView.
I also have absolutely no idea how to get the thing to update when the variable changes.
Any assistance in this would be great, it has taken all day so far and I feel nowhere closer to solving the issue.
I do appreciate your time and effort.
There a many tutorials in PaintCode home page and at this other website, good lucky
However to access the values you create you just need to create an outlet from the UIView to your UIViewController class and class myViewName.myVariableName = myValue
As this step by step tutorial explain
The only tutorials I've found are the PaintCode website.
There are three things that I tend to adjust when trying to add/remove parameters to make my drawings more customizable via code.
When you click on a canvas, there is a drop down menu in the inspector that allows you to include a draw method, an image method, or both when you export the style kit.
When selecting the color in the color palette, there is a drop down menu in the upper right that allows you to make the color a parameter, so that when you export, you can set the color via code.
Add a frame to the canvas and set the constraints in the inspector. These constraints are the old style layout constraints that are very annoying, but doing this inserts a rect parameter in the exported methods which you can set in the code as well.
I don't know any other good PaintCode tutorials - over and above - the tutorials suggested. But after some research, I think I know the answer to your problem:
Background before the answer: I had the same problem. For example, you have a good sized image (created by PaintCode) that works on all iPhone device except the iPhone 4S.
To solve my issue:
Open PaintCode & find the “Frame” button.
Learn a new word “Parametric”. That is what we are dealing with. When the frame changes in size, you set whether the objects inside the frame change size or stay the same.
Watch the Dynamic Shapes tutorial on the PaintCode site. All of the answers are within this short youTube video. It took 5 times of watching it until I have extracted all of the information I needed to get it to work.
If you want the more details, keep reading:
#IBDesignable
class beerViewClass: UIView {
override func drawRect(rect: CGRect) {
SecurityStyleKit.drawCarDashboard(frame: self.bounds, wageIncome: "yo!")
}
}
Notice I can set the frame size as I have multiple views of the same image. Also I can send in variables to the drawCarDashboard.
Within PaintCode, put the objects you want to scale / shrink inside the frame.
Set the Springs and Struts for each item in the frame. For example, do you want it to shrink, stay vertically centered, etc.
Export your StyleKit to Xcode.
in Xcode, using Storyboard or code, add the UIViews to your View
Controller
Add a new Swift file.
Change the blank Swift file to a subclass of UIView & add code (see example)
In storyboard view, select the views that were added.
Set the view to the class you wrote (beerView)
I hope that helps. I hope my explanation was clear.

How to make the buttons visible on simulator?

I'm using Xcode 5.1.1. I recently added a background image to my application but the buttons on the view controller aren't visible. When I remove the background image, the buttons become visible again. How to handle this?
If you are not checking the view hierarchy, then do one thing create outlet of you button and then write following code in viewDidLoad.
[self.view bringSubviewToFront:yourButton];
I don't know if you were used xib...
If you are using xib in your project, giving a background image for your button will more easy.
Place a button anywhere you want. Then go to attributes inspector on the right hand side. There you can find a field for changing your background image. Just give the name of your image.
Hope it will helpful for you

Unable to change the view size in XIB?

I am trying to make the view controller as size as 300x400, i have done following things,
Open the XIB and go to the Size inspector
Changed the width and height properties to 250 and 300
Saved the file and closed interface builder
But its Not working ??
Just double check with your code whether anywhere frame assign programmatically. If NO, just do following step.
1) Clean and build -> Run. If not work, go to second step.
2) Delete app on simulator and run again.
Right click on XIB
Choose open As
chose source code
and change width and height ..
First of all its important to know that how you are checking size is changed or not.
If you are doing addSubview of this resized xib into other view controller than you feel the change in xib. But in case you are using Push view controller than you will not feel the change. Because view controller will show over the window so you can not change the window size.
Note :
If you want to show the resized view as an subview in any other page than you need to take subview in that view & make add subview of the current view.
Hiii #Gautam, this can be possible, you can do the following things,
Clean your project because sometimes it will not recompiled. Run it again. If this not work then you can use 2nd way
Open the source code of filename.xib by using right clicking that xib and perform change to height and width.
Hope this will work for you...
Thank you...

Single view iOS app with custom drawRect

I though I understood this, but I can't get it to work:
I'm trying to create a very simple app, to test various things (OK - it's an app to estimate Pi using the Monte Carlo technique by simulating throwing darts at a board).
I have a single view iOS app (e.g. from the single view template) on which I've simply got one UIButton (to launch the app) at the bottom and a UILabel at the top to show the results.
The view controller is a custom subclass of UIViewController call PiCalcViewController; the view is a standard UIView filling the whole screen.
The app works but now I want to be able to draw a graphic representation of how the simulation is going the middle of the screen, which I'll do in my view's drawRect (right?). So I thought that I'd create a new file (PiCalcView) an Objective-C class, make it a subclass of UIView and then, in IB, drag out a new view in the middle of my view controller and change it's class to PiCalcView.
Great, except that PiCalcView does not appear in the drop-down class list in the inspector.
Questions : Any idea what's wrong and is creating a subview of my UIView like this the right way to do it?
Type the class into the custom class section in the Identity Inspector.
OK - quitting and restarting Xcode appeared to do the trick.
Thanks for the tips - being new to Xcode & ObjC, I didn't know if I had the right technique.At least I know now.

Resources