Move UIView to another UIView - iphone

I have an UIViewController which contains 2 UIScrollViews, scrollView1, scrollView2 for instance.
scrollView1 contains many UIViews, when tapping one of it's UIViews I want it to move into scrollView2
When tapping a UIView that belongs to scrollView1, a method inside the UIViewController is being called and the view is passed as a parameter.

In that method you should write something like:
[view removeFromSuperview];
[scrollView2 addSubview:view];
Edit:
For animated move you should try something like:
CGPoint originalCenter = [self.view convertPoint:view.center fromView:scrollView1];
[view removeFromSuperView];
[self.view addSubview:view];
view.center = originalCenter;
CGPoint destinationPointInSecondScrollView = ; // Set it's value
CGPoint finalCenter = [self.view convertPoint:destinationPointInSecondScrollView fromView:scrollView2];
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
view.center = finalCenter;
} completion:^(BOOL finished) {
[view removeFromSuperView];
[scrollView2 addSubview:view];
view.center = destinationPointInSecondScrollView;
}];

Supposing you have these two scrollViews declared as properties:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTapped:)]
for (UIView *view in self.scrollView1.subviews) {
[view addGestureRecognizer:gesture];
}
}
- (void)viewTapped:(UITapGestureRecognizer *)gesture
{
UIView *view = gesture.view;
[self moveToScrollView2:view];
}
- (void)moveToScrollView2:(UIView *)view
{
[view removeFromSuperview];
[self.scrollView2 addSubview:view];
}

Related

View goes out of a View Boundary

I have a Background view (UIImageView) in that i am adding a view(UIView).
this contentView(UIView) contains UIPanGestureRecognizer.
I am giving UIPanGestureRecognizer to contentView by bellow code :
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(movePhoto:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[contentView addGestureRecognizer:panRecognizer];
[panRecognizer release];
and then i am adding this contentView to Background View (UIImageView) by bellow code :
[imgBG addSubview:contentView]; // here imgBG is Background view.
but when i move this contentView it goes out of imgBG.
bellow is the method to move contentView
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
CGFloat finalX = [sender view].center.x;
CGFloat finalY = [sender view].center.y;
[[sender view] setCenter:CGPointMake(finalX, finalY)];
I think, here the contentView should remain inside the imgBG as it is the subview of imgBG.
Please let me know whether there is a problem in my code or its due to UIPanGestureRecognizer.
thanks in advance.
I just had to do :
[imgBG setClipsToBounds:YES];

Gesture recognizer and UIImageView

I found this code to change the image when it is clicked.
in .h
#interface MyappViewController : UIViewController
{
NSDictionary *ddata;
UIImageView *firstImage;
}
#property(retain,nonatomic) IBOutlet UIImageView *firstImage;
in .m
- (void)viewDidLoad
{
[super viewDidLoad];
firstImage.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(clickHandler:)];
pgr.delegate = self;
[firstImage addGestureRecognizer:pgr];
[pgr release];
// [self clickHandler:self];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
/* destroy the transition view, set the image first */
UIImageView *transitionImageView = (UIImageView *)context;
self.firstImage.image = transitionImageView.image;
[transitionImageView removeFromSuperview];
transitionImageView = nil;
}
- (void)clickHandler:(id)sender {
/* temporary view for the animation */
NSLog(#"Click Handled ");
UIImageView *transitionImageView = [[UIImageView alloc] initWithFrame:self.firstImage.frame];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[ddata objectForKey:#"pic"]]]];
transitionImageView.image = image;
transitionImageView.alpha = 0.0f;
[self.view addSubview:transitionImageView];
[UIView beginAnimations:#"UpdateImages" context:transitionImageView];
[UIView setAnimationDuration:2.0f];
transitionImageView.alpha = 1.0f;
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
When I click the image nothing happens, but if I call [self clickHandler:self]; in ViewDidLoad, the image changes. My problem is that the click is not handled when I click the image.
Instead of a UIPinchGestureRecognizer you need to use a UITapGestureRecognizer. Don't forget to set things like the number of taps required and number of fingers either. The docs are very good for gesture recognizers.

UITabBar wont hide

I have a UINavigationController in a UITabBarController, and I can't seem to get a pushed viewController's tabBar to hide.
I am using the following code to hide it:
Before it gets pushed:
tpsv.hidesBottomBarWhenPushed = YES;
tpsv.tabBarController.hidesBottomBarWhenPushed = YES;
viewWillAppear:
self.tabBarController.tabBar.hidden = YES;
AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[[del tabController] tabBar]setHidden:YES];
But none of the above work.
If you could tell me how to fix this, that would be great.
You set this before you push the new view controller:
MyViewController *myVC = [[[MyViewController alloc] init] autorelease];
myVC.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:myVC animated:YES];
[EDIT: comment re usage]
Just noticed you say you tried this. Not sure what else you're doing in the context of pushing your VC or configuring it but this does work fine. It's how I do this exact thing in my apps.
- (void) hideTabBar:(UITabBarController *) tabbarcontroller {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
[UIView commitAnimations];
}
- (void) showTabBar:(UITabBarController *) tabbarcontroller {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
NSLog(#"%#", view);
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
}
}
[UIView commitAnimations];
}
I've faced the same problem with
myVC.hidesBottomBarWhenPushed = YES;
It does'nt remove the tab bar in subsequent views. May be it's deprecated. You should'nt face this problem with the setHidesBottomBarWhenPushed: command. Try using the following for views:
MyViewController *myVC = [[[MyViewController alloc] init] autorelease];
[myVC setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:myVC animated:YES];

Nested Views-button is not clicking

i have a view which has a datepicker and a button added to it.
There is another view which adds the above view as subview.
But the events like touchesBegan and button's action are not being clicked on the subview.
Please help
The code of the parent view is:
iTagDatePicker *dt=[[iTagDatePicker alloc] initWithFrame:CGRectMake(0.0, 180.0, 320.0, 240.0)];
//dt.userInteractionEnabled=YES;
//[dt becomeFirstResponder];
dt.backgroundColor=[UIColor clearColor];
[UIView beginAnimations:#"animation" context:nil];
[UIView setAnimationDuration:1.0];
CGPoint cntr;
cntr.x=160.0;
cntr.y=420.0;
dt.center=cntr;
[self.view addSubview:dt];
self.view.userInteractionEnabled=YES;
CGPoint cntr1;
cntr1.x=160.0;
cntr1.y=158.0;
dt.center=cntr1;
[UIView commitAnimations];
[dt release];
and the code for the sub class is:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
dtPicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, 320.0, 216.0)];
dtPicker.datePickerMode=UIDatePickerModeDate;
dtPicker.date=[NSDate date];
[self addSubview:dtPicker];
btn=[[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
btn.frame=CGRectMake(110.0, 400.0, 100.0, 20.0);
[btn setTitle:#"Done" forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
[btn becomeFirstResponder];
[btn addTarget:self action:#selector(SelectedVal:) forControlEvents:UIControlEventTouchDown];
[self addSubview:btn];
}
return self;
}
The button is not working
Subclass the parent view and pass touches to its children views.

iphone: rotating flipped view when rotating visible view

I have a resizing issue when rotating a view controller that has two views which I switch between using the flip animation.
The problem appears if i do the following steps:
Rotate the device when viewing the tableview.
Click the info button.
Rotate the device (infoView appears stretched).
Click on info button (tableview appears stretched)
It appears that the view that is not added to the superview is not resizing correctly, since it was not part of the composite views when the device was rotated.. Is there any way to get this view auto resized correctly?
Below is a code sample
- (void)viewDidLoad {
[super viewDidLoad];
//background image
backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg-app.png"]];
backgroundImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
[self.view addSubview: backgroundImageView];
[backgroundImageView release];
//infoView
aboutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"About.png"]];
aboutImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
//tableView
self.tableView = [[[UITableView alloc ] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease ];
//set the rowHeight once for performance reasons.
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = 75;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizesSubviews = YES;
//set the rowHeight once for performance reasons.
[self.view addSubview: tableView];
//[tableView release];
[self.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];//for resizing on rotation
//info button
UIButton * infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoDarkButtonType.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
infoDarkButtonType.backgroundColor = [UIColor clearColor];
[infoDarkButtonType addTarget:self action:#selector(infoAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonInfo = [[UIBarButtonItem alloc] initWithCustomView:infoDarkButtonType];
self.navigationItem.rightBarButtonItem = buttonInfo;
[infoDarkButtonType release];
self.navigationItem.rightBarButtonItem = buttonInfo;
[buttonInfo release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
- (void)infoAction:(id)sender{
NSLog(#"Clicked on the info button");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75]; /* Sub. duration here */
UIView *superview;
if ((superview = [tableView superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:superview cache:YES];
[tableView removeFromSuperview];
[superview addSubview:aboutImageView];
} else if ((superview = [aboutImageView superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:superview cache:YES];
[aboutImageView removeFromSuperview];
[superview addSubview:tableView];
}
[UIView commitAnimations];
}
Thanks
Try putting the code in viewWillAppear:animated. It's called every time your view will appear. View did load is called once.