I have UIImageView ( Say A ) and UIView ( Say B ) added on UIView ( Say Parent ).
Now A and B are both added on Parent and A is on top of B.
Some portion of A is transparent,now I am looking to pass all gesture clicked on A in transparent area should be passed to B.
Is that possible?
Related
I am developing an application in which i am creating small sub views in the same view.
What i want is when i change the value of alpha of the view, the value of alpha for sub view is also getting changed which i do not want.
How to implement the code for changing the value of the view not the small sub views created.
Thanks,
You cannot do that, the alpha is inheritate, so what you have to do is to change the view structure from
YourMainView -> Your SubView
To
ContainerView ->YourMainView
-> Your SubView
So now YourMain View the one that you want to apply the animation, is not the paret view of your current subView, the both views are sibilings, also ContainerView will have a clear background, so it wont affect it will just contain both the views
As suggested already, you should not add YourSubVew as subview of YourMainView.
Make both of your views a subview of an empty and transparent common subview (ContainerView) which has the same size and position that YourMainView currently has.
ContainerView -> YourMainView
-> YourSubView
Make sure to add YourMainView first as subview of ContainerView before you add YourSubView as subview of ContainterView. Otherwise YourMainView may overlap/hide YourSubView.
Doing so you can set both views' alpha independend from each other.
(Omar's answer is quite correct, but his "graphical visualisation" is misleading and it missed the point of the sequence of the subviews.)
Edit:
What you have today is like this:
yourMainView.alpha = 1.0;
...
[yourMainView addSubView:yourSubView];
yourMainView.alpha = 0.1; //probably with animations etc.
What happens is, that yourSubView will "inherit" the alphaValue of its superview.
ANd there is even more to that. Assuming that your subView has an alpha of 0.5, that would effectively become 0.05 when when its superview's alpha is set to 0.1.
Change that code to:
containerView = [[UIView alloc] init]; //you may choose a different init method
//If you use initWithFrame then use the frame of yourMainView.
//However, make sure that ContainterView is of the same size as yourMainView and has the same position.
[containerView addSubView:yourMainView];
yourMainView.alpha = 1.0;
...
[containerView addSubView:yourSubView]; //add it to the container too!
yourMainView.alpha = 0.1; //this will now effect the MainView only.
That is basically all the trick. Views can perfectly well overlap each other without being subviews. Make them subviews only then when they really are subveiws, when they are moved togehter, appear and disappear together, etc.
I have 25 UIControls and 10 UIImageView. It looks like this:
Now the images are the UIImageVIews and the grids are the UIControls. What I want is when the user taps at a image and then taps at any of the grids (any blank grids of-course) the UIImageView is then removed from the current superView and added as the subView of the blank grid tapped. For example, if the user taps at the A1 grid first and then in the B2 grid, two actions occur,
UIImageView from A1 grid is removed,
UIImageView is added to B2 grid
This means when the user taps at the image at A1 grid and then taps at the B2 grid, the output window looks like this:
EDIT
So, at a time I need to have track for two UIControls:
1.which one is pressed first, and
2.which one is pressed second. And then remove the UIImageView from the first UIControl and add it to the second UIControl. I have tried giving the UIControls a tag like,
//....
A1.tag = 1;
//...
-(void)a1ViewTapped:(id)sender
{
int i = A1.tag;
[self switch:i];
}
//...
-(void)switch:(int)_tag
{
//....
UIView *view = (UIView*)[self.view viewWithTag:_tag];
[view removeFromSuperview];
//...
}
By this I can track a single UIControl, but need to track two of them.
How can I accomplish this, can anyone help?
UIControl inherits from UIView, so you can add a subview to your UIControl by calling addSubview:.
You can remove your UIImageView from the previous control by calling removeFromSuperview on the UIImageView
edit:
A very simple implementation would be to create a property that holds a reference to the selected UIControl. When the property is nil, then you are in selection mode. Touching up inside a control puts a reference to the control into the property. When the property is not nil, you are looking for a grid control in which to place the image.
You'll also need a way to keep track of whether your UIControl has one of the UIImageView instances as a subview. There are a number of ways to do that. One way would be to subclass UIControl to add a property to it that points to the UIImage it contains. Another way might be to simply examine the subviews of the UIControl to see if it contains one of the images in question. Another way would be to create some data structure to keep track of the positions of the images. How you do it is up to you, and the best way depends on the specifics of your implementation.
Just for the sake of discourse, let's assume you set up an NSMutableDictionary called imageMap that uses the tags of your UIControls as keys and sets references to the images as values. Then you could do something like:
-(void)selectGridControl:(UIView*)view
{
if (selectedControl == nil) // In this case nothing is selected
{
selectedControl = view;
}
else // In this case we are moving the image to the new view
{
UIImageView *selectedImage = [imageMap objectForKey:selectedControl.tag];
if (selectedImage != nil)
{
[selectedImage removeFromSuperview];
[view addSubview:selectedImage];
[imageMap removeObjectForKey:selectedControl.tag];
[imageMap addObject:selectedImage forKey:view.tag];
selectedControl = nil;
}
}
Initializing imageMap and handling cases when the destination control already has an image are left as exercises for the reader.
Why is my custom UIView not rendering within the UIView container of it's parent? So what I have is:
MainViewController - has a UIView "customViewContainer" (which doesn't take up the whole screen), and
CustomView - is a UIView with a XIB file - it is the UIView here that when it is rendered (with AspectFit) is rendering outside the bounds of the parent "customViewContainer"
The code used to setup the custom view is this extract from MainViewController:
<< cut - see Update2 below >>
So I can't see why the CustomView view is being rendered in a way that is larger in area than the parent customViewContainer? What I want is for the customview to fit into the parent "customViewContainer" entirely per the AspectFit type approach.
thanks
EDIT 1 (added clarification) - If I "clip subviews" in the parent view then it does then clip things, but what I really need to render the custom view within the parent view area (not the whole area of the screen). So I need (a) the center of the custom view to be in the center of the parent view, and (b) custom view to AspectFit into the parent view properly. Any ideas?
EDIT 2 - Update
sorry - made a copy/paste mistake with code in the original question - can't seem to edit it so I'll put a correct version below - so to clarify:
MainViewController - has a UIView "containerView" (which doesn't take up the whole screen), and CustomView - is a UIView with a XIB file - it is the UIView here that when it is rendered (with AspectFit) is rendering outside the bounds of the parent "containerView"
With the code below does this make sense now? The reason for this code is I have a custom UIView, BUT I have a XIB file associated with it, so this was the only way to get my MainController view to be able to use it. That is, have a container view in the MainController view, and then programmatically add the CustomView into the container view.
Re "So set the frame or center of the view that you're adding to be what you want it to be" - are you saying I have to programmatically/manually set the dimension of the CustomView to be what I want (in relation to the parent containerView)?
What I was hoping was there was a way using the declaritive layout setting to some how be able to say "Load the Custom View from it's XIB file, and the aspectFit this view into the self.containerView", however I'm starting to wonder if this is possible?
UPDATED CODE BELOW (made mistake in original question when I copy/pasted it in and changed variables names etc)
- (void)viewDidLoad
{
[super viewDidLoad];
// Load the custom Altimeter View into this UIControllerView's container UIView for it
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:#"Customview" owner:self options:nil];
for (NSObject *obj in nibs) {
if ( [obj isKindOfClass:[Customview class]]) {
Customview *cv = (Customview*)obj;
[self.containerView addSubview:cv];
break;
}
}
// UI Layout
self.containerView.layer.borderWidth = 2;
self.containerView.layer.borderColor = [[UIColor redColor] CGColor];
}
Check if the "Clip subviews" property of parent on the IB file is checked.
I think the equivalent code is self.view.clipsToBounds = YES.
if this is NO, subviews that draws outside will be visible as if it's drawn on the parent.
I have a UIImageView (imageView) being the subView of the UIScrollView (scrollView). The scrollView is set only for pinching the showed image from imageView. And I have a small UIImageView (iconView) which is the subView of imageView. So the relationship is like:
UIView
|_____ UIScrollView ( scrollView )
|_____ UIImageView (imageView)
|______ UIImageView(iconView) .
I need to touch and move the iconView but I am confused with this two methods I found.
Can I simply use the "hitTest:withEvent:" method to work this out and how to use hitTest ?
Or Should I override "UITouchesBegan:withEvent:" "UITouchesMoved:withEvent:" "UITouchesEnd:withEvent:" ? and how to use it?
You should use the 2nd method.
Subclass the UIView class and create CustomView. Define iconView as CustomView.
In CustomView define the 3 delegates to the desired behaviour:
UITouchesBegan:withEvent:
UITouchesMoved:withEvent:
UITouchesEnd:withEvent:
Hope this helps
so lets say I have a UIScrollView, within it are 3 UIViews, within those there is a UISlider in each one. they are positioned vertically in the UIScrollView.
I now have a 4th UIView also in the UIScrollView which I wish to move around depending on the position of the slider which has been used.
so within my sliderChanged method which i pass the sender, i get the position of the slider, and adjust the position of the 4th UIWindow to its y. This works great on the first UIView, but once on another UIView which has forced me to scroll down, using the slider moves the 4th UIView but stays at the beginning of the UIScrollView
I am using:
[4thView setCenter:CGPointMake([4thView center].x, [slider center].y+10)];
what I need is to get the position of the slider relative to the content of the scrollView and not relative to its UIView, so that I may set the 4th view again relative to the scrollView content.
You can convert the points by UIView's instance methods.
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
For example, I want to convert a ponint on the viewA to the scrollViewB's coordinates.
CGPoint aPtInScrollView = [viewA convertPoint:aPoint toView:scrollViewB];
Or, I want to know the position of viewA in scrollViewB.
CGPoint aPosViewA = [scrollViewB convertPoint:CGPointZero fromView:viewA];
Using the previous answer I took it a bit further to solve the issue I was having.
If you have multiple UITextView's within multiple UIView's all inside a single UIScrollView: this will animate and scroll to them.
This could be also be applied to UITextField's as well.
-(void)textViewDidBeginEditing:(UITextView *)textView {
[self.theScrollView setContentOffset:[self.theScrollView convertPoint:CGPointMake(textView.frame.origin.x, textView.frame.origin.y) fromView:textView.superview] animated:YES];
}
If you have labels above your textviews, just offset the x or y by that amount.