I have two circle objects on main view with coordinates: first.frame 100,100,20,20 and second 200,200,20,20
Main view frame is 0,0,320,480
I want to create another view with frame 50,50,200,200 and add my circles to that view, but that the new position was the same in relation to the main view
If I use this code I have:
...
[self.view addSubview:anotherView];
for (CircleView *circle in self.view.subviews)
{
if ([circle isKindOfClass:[CircleView class]])
[anotherView addSubview:circle];
}
My circles is replace to another view, but coordinates is same like a left-up corner not main view, another view
How to replace my objects to another view with same coordinates
You can use convertPoint:fromView: or convertPoint:toView: something like this:
CGRect newCircleFrame = circle.frame;
newCircleFrame.origin = [self.view convertPoint: circle.frame.orign
toView: anotherView];
Related
I have 1custom UIView in my default view in viewcontroller.My need is that if a button is clicked my default main view goes to blur and custom view remains unchanged.And a button is clicked again the blured view returns to normal effect. I used the UIBlurEffect and UIVisualEffectView for making blur. But the problem is whole view got blured.
I've not tried the effects views yet, but they look like they participate as good citizens in the view hierarchy. If that's true, then this should work...
// assume aView is the view you want blurred, and
// aSubview is the view you want to remain unblurred
[aSubview removeFromSuperview];
// apply the effect view to aView
[aView addSubview:aSubview];
Apply it to your view only you want to make it blur. Try it:
UIVisualEffect *MyblurEffect;
MyblurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:MyblurEffect];
visualEffectView.frame = MyVIEW.bounds;
[MyVIEW addSubview:visualEffectView]; //MyVIEW-> your view
Try with these link:
How to apply blur to a UIView?
I'm working on a cocoa app, in which i wanted to dynamically add, remove, resize views.
Shown above is the image, in which there are three views in a parent view out of which view-B can be added or removed and based on that we need to resize view-C.
Any cocoa/objective-c help for this.
//To add a subview
[parentView addSubview:subview];
//To remove a subview
[subview removeFromSuperview];
//To resize a view
[subview setFrameSize: NSMakeSize(width, height)];
[subview setFrameOrigin: NSMakePoint(originX, originY)];
If you need to set the frame of a view based on the size or position of other views, you can use a views frame which has a size and an origin etc...
For example, to move C so it borders A and takes up the rest of the space in the view:
NSSize size = NSMakeSize(parentView.frame.size.width, parentView.frame.size.height - a.frame.size.height);
NSPoint origin = NSMakePoint(a.frame.origin.x, a.frame.origin.y + a.frame.size.height);
[c setFrameSize: size];
[c setFrameOrigin: origin];
I'm adding an MPMoviePlayerController to a container view like so:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
. . .
self.moviePlayer.view.frame = self.frame;
[self addSubview:self.moviePlayer.view];
The self is the container view. This container view is instantiated off-screen when it is created by a view controller. So the container view resides in a view, and the MPMoviePlayerController resides in the container view.
When I animate the container view on screen, it works. Other views slide off and this view slides in. However, the MPMoviePlayerController stays put with its frame off-screen. So the MPMoviePlayerController's container view moves on screen, but it does not move with its container view. Is this not allowed, or am I missing something? The code for animating the container view is below:
[UIView animateWithDuration:ANIMATION_LENGTH animations:^() {
for(int i = beginTag; i < beginTag + [self subviews].count; i++) {
UIView *viewPointer = [self viewWithTag:i];
CGRect viewFrame = viewPointer.frame;
viewFrame.origin.x -= SCREEN_WIDTH_VC;
viewPointer.frame = viewFrame;
}
} completion:^(BOOL finished) {
[self.vidLoad playVideo];
}];
This takes care of animating elements that are on-screen off of the screen, and then bringing off-screen elements onto the screen. I'm glad to hear all suggestions!
Solved - this was a result of my own stupidity. The view hierarchy was indeed working, but when I assigned self.frame to moviePlayer.view.frame, it took the absolute coordinates of the container view, not relative coordinates.
A silly mistake. Basically, I just had to write
self.moviePlayer.view.frame = CGRectMake(0, 0, SCREEN_WIDTH_VC, SCREEN_HEIGHT_VC);
This would place it right on top of the container view, as these coordinates are relative to that container view and not the screen (I just confused myself).
Well, this is what I want to do in my app: I would like to implment a UIView with a map on the top half of the screen and a tableview on the other half with two buttons in the middle. If I press one of the buttons the map will get fullscreen and if I press the other one the tableView will fit all the screen.
Any suggestion?
In one view controller like a UINavigationController create an MKMapView with a frame the size of the top half of the view and add it as subview of your view controller. Then I would create a UIToolbar to hold your buttons and make the top of it's frame line up with bottom of the MKMapView. Finally create a UITableView with it's frame just below the others (make sure you hook up it's delegates).
Then assign the target of your UIBarButtonItem that makes the map go fullscreen to a method that animates the frames of all three views like this:
[UIView animateWithDuration:0.24
delay:0.0
options:UIViewAnimationCurveEaseOut
animations:(void (^)(void)) ^{
self.toolbar.frame = CGRectMake(0, MAP_HEIGHT_FULLSCREEN, 320, TOOLBAR_HEIGHT);
self.mapView.frame = CGRectMake(0,0,320,MAP_HEIGHT_FULLSCREEN);
self.tableView.frame = CGRectMake(0, MAP_HEIGHT_FULLSCREEN+TOOLBAR_HEIGHT, 320, MAP_HEIGHT_FULLSCREEN-MAP_HEIGHT);
}
completion:^ (BOOL finished){}
];
Create both views how you are planning, On one button click, change the frame of one view to fit the full screen, if you click the other button, do the same thing to the other view.
I have an application which loads a view in it. I want to position the loaded view in 450pt of y cordinate. How to do that.
Regards
Ranjan
Look at the documentation for UIView and in particular the properties frame, bounds and center
I assume that you are adding a subview so you want it in the coordinate that is relative to the parent view. Then you use frame.
CGRect r = [subView frame];
r.origin.y = 450.0f;
[subView setFrame:r];
Something like that.