Adding a UIView as a subview to UIImageView - objective-c

I am working on creating a color picker for "iOS". I am using this project, since it does what i want: "This"
I want to create a moveable circle (UIView) on top of the palette (UIImageView).
What I want to do is, while users move the circle, take the touch point and call the method getPixelColorAtLocation(); and change the background color of the circle to the color on current point. (Seen on most of the color palette/wheels)
The method getPixelColorAtLocation() is available on a child view. I created a circle with UIView on parent view, the problem is I cant call to getPixelColorAtLocation() from parent view.
My question is, Is there anyway to add a UIView as a subView to UIImageView. If I cant, what choices do I have to achieve what I want?

Yes you can do this.
[myImageView addSubview:sampleView];

An image view is just a subclass of UIView, so you can add subviews to it however you'd like. If there's a reason why you can't do so, then you can always make it a subview of the image view's superview, move it to the front, and then use convertPoint:toView: to convert to the image view's coordinate system, then get the pixel color.

Related

Objective C draw in view dynamically

I have seen many examples of views being subclassed to override drawRect, but that approach is pretty static (at least as far as I understand).
What I'd like to do is set up a very simple drawing canvas. In that, I've got a view with a UIPanGestureRecognizer attached to it. Whenever the gesture fires a new position, I'd like to draw a circle of a fixed size and color in that position of the view. The gesture recognizer is attached to the view, but it fires a selector in the view controller. I already have a subclass of a UIView. So, what would be the best approach?
Thanks.
What you need to do in this case is still to override drawRect!
The difference is that you, when recognising gestures, need to keep track of the location(s) in which this circle should be drawn, and access that information the next time you are redrawing the view, essentially building up an image in memory that you draw into the view.

UIImageView overlapping effect inside a UIView

Check this URL http://krystalrae.com/
Scrolling down you will see a girl's cloths are changing on mouse wheel scrolling.
I have to create a iPad application where 3 images will appear likewise. The difference will be that in my case image change will occur horizontally rather than vertically in website. Also, i have to make it inside UIView with events touchesBegan, touchesMoved, touchesEnded and touchesCancelled
Help will be appreciated.
Thanks.
From your comment I gather that the problem is the resizing of the images in UIImageView when changing the bounds of the view.
You can set the contentMode of the image view to something else. The default is UIViewContentModeScaleToFill which is not what you want.
You could, for example, set the lower image view to UIViewContentModeBottom, and the upper image view to UIViewContentModeTop.
Don't forget to make sure your image views have sett the property maskToBounds set as YES.

How to prevent UIView background from rotating? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can I rotate a UIView without the black bars?
I want the labels on my UIView to rotate (and they do) but I've set the background to a color other than black. When I rotate you can see the view's rectangle rotate against a black background. How can I keep the UIView's background still yet allow the rest of the subviews to rotate normally? And what is that black background behind everything? Do I have access to it?
I'm not sure I fully understand the question. What I think your question is (if I'm misunderstanding please clarify where I'm getting the question wrong):
You have a UIView that is the same size as the device's screen.
You want to rotate that view.
When you rotate that view the background of the view's parent view can be seen.
I think the only ways to resolve this are either:
A) Make the view you're rotating larger than the screen so that when you rotate the parent view isn't visible. If you do this make sure the parent view isn't set to clip it's subviews otherwise you won't see any difference after resizing.
OR
B) Make the parent view's background color match the color of the view you're rotating. If you don't want to permanently alter the background color of the parent view you can always change the color when you start the animation and revert to the original color when done.
P.S. - I think the "black background behind everything" is the background color of your app's UIWindow. If you want to change that you can do the following (assuming your app delegate has a window property defined but if you used one of the standard Xcode templates to create your app it should):
UIWindow* window = [[UIApplication sharedApplication].delegate window];
[window setBackgroundColor:<Some UIColor>];

Manually scrolling a UIView contentOffset

I'm trying to scroll the contents of a UIView using the contentOffset which so far I achieve handling touchesBegan: and touchesMoves: methods of the view, however I get a jumpy effect as I'm changing the bounds of the UIView manually (for example: current position +/- the change of the last position of the finger and the new position)
Is there any easy way to achieve this without manually changing the bounds of the UIView?
IMPORTANT NOTES: I'm not using a UIScrollView because the mentioned UIView has plenty of draggable subviews so if I use a UIScrollView I can't drag because the UIViewScroll scrolling event executes over the subviews events.
Animating the layer of the mentioned UIView causes the contents to move out of control, like if the point of reference had changed somehow.
However I'm always open to suggestions.
I would rather make a UIView container clipping subviews, add the content view which we will drag as a subview and change it's frame. I think it shouldn't have jumpy effect as it must be the same as animating the view frame change.

Rounded corners on NSTableView

I have a custom view subclass similar to NSBox that draws a rounded box background. The problem is that if I place a view like an NSTableView in the box view, it does not clip to the rounded corners. Is there any way to round the corners of NSTableView and its parent scroll view?
I haven't tried this with a table view but have with other controls.
In a subclass of NSTableView (or whatever view/control you want to clip)
Override drawRect:
Create an NSBezierPath with the shape you want (probably appendBezierPathWithRoundedRect:xRadius:yRadius: just remember to use the view's bounds as the size)
Send the path the addClip message to add that shape to the view's clipping path
Call super's drawRect:
If the table view has a header you may need to clip the top corners by subclassing NSTableHeaderView. And if you have scrollbars you may have to do the same thing to them except only clip certain corners. Hopefully you don't have scrollbars because I doubt that would look right. Basically you want to clip the view/control that draws that part, clipping the parent will not cause subviews to be clipped.
If you look at Apple's Welcome to Xcode window they get away with it by drawing a custom header at the top and a text block at the bottom so they don't have to round the table view itself. If you can do something like that I would.