how to scroll to the center position of the object - ios

Hi Im making a simple game with the basic UIKit stuff where there is an object that pops up on the screen.
I'm trying to make it so that every time the object pops up the screen will scroll to the object and center it on screen. Basically I have a UIView that has the objects that popup within it and that UIView is a subview of a UIScrollView. All of this is a subview of self.view of my ViewController. I'm using the UIScrollview to scroll through the contents of its subview with the objects in it. I'm not sure if a UIScrollView is the ideal option for what I want to do but I was looking for a way to scroll the position of the object and center it in the middle of the device. Ideally with animation.
If anyone knows of a method or maybe a formula that could help me out that would be awesome. Thanks in advance :D
Update:
I don't really have much code in regards to this except for adding the views within each other :
self.world = [[UIView alloc]initWithFrame:CGRectMake(0, 0, currentDeviceWidth*4, currentDeviceHeight*2.2)];
self.world.backgroundColor = [UIColor blueColor];
_scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
[_scrollView setContentSize:self.world.frame.size];
[_scrollView addSubview:self.world];
[[self view] addSubview:_scrollView];
but below is a simple image of what I want to accomplish. The arrows are just a representation of the possible scrolling direction.
Apple object is off the side of the screen Note: apple object is a subview of self.world and self.world is a subview of scrollview
The finish look is where now the scrollview has scroll self.world to where the apple object is in the center of the screen
Just looking to basically accomplish how a user could manually scroll the object to the center but instead do it automatically without the use of a user's touch.

I think you may need contentOffset which is a property of UIScrollView. This property controls the position of the contentView.
Now let's say, your apple is in the center of the self.world, then animated put it in the middle is like this:
[UIView animateWithDuration: 0.5//you can put any float as you want
animations:^{self.world.contentOffset = CGPointMake((_scrollview.frame.size.width - self.world.frame.size.width) / 2, (_scrollview.frame.size.height - self.world.frame.size.height) / 2);
}];
If you put your apple in any other position, you should calculate the position yourself, and set the contentOffset to the right one. I think any position other than center is much complicated, you calculation should involve the position of the apple in the self.world view.

Did you try
[_scrollView scrollRectToVisible:newView.frame animated:(BOOL)animated]
What might be tricky about what you are trying to do is positioning views such that they are always within the coordinate system of the _scroll view, e.g. x >= middle of the view & y >= middle of the view.

Related

Moving UIView from One Tab to another

I'm making this type of screen.
I've used 3 UIButton and programmatically gave them border. The little red line is a UIView. I've provided a constraint to UIView Equal Widths as Trending Button.
Now my question that when user taps the next button this UIView should move to the next one. View Controller is 600x600 and next button is at 200x0 so if I change the value of UIView to 200 that will be hard coded and it'll alter according to the screen size. I want it to be perfect and don't know any other way.UPDATE:I'm using AutoLayout so for this purpose I used [self.buttonBottomView setTranslatesAutoresizingMaskIntoConstraints:YES]; and when I run the app the buttons were messed up like in the screenshot. I've applied some constraints on buttons.
You can use NSLayoutConstraint ,change it's LeadingConstaint when you tap on the next button.
Code:
- (void)changeSelectedByTapView:(UIView *)tapView {
_currentSelectedView = tapView;
self.lineImageViewLeadingConstaint.constant = tapView.frameX;
self.lineImageViewWidthConstaint.constant = tapView.width;
[UIView animateWithDuration:0.5f animations:^{
[self.lineImageView.superview layoutIfNeeded];
}];
}
change frame of your UIView.
view.frame = CGRectMake(0+buttonWidth*i, y, width, height);
i is index which is 0 for Trending, 1 for Made by the fans and 2 for your rules.
Also when you change frame for your view, it is shown only on one button at a time.

iOS UIView 3D Rotate and SubViews

I am easily rotating my UIView using CATransform3DMakeRotation but the problem is occurring when em adding a SubView after the rotation. It is adding it as rotated..
This is my view before the rotation
This is how i am rotating my UIView
aCustomView.layer.transform = CATransform3DMakeRotation(M_PI,0.0,1.0,0.0);
and after that em adding a custom Badge on it by this
JSCustomBadge *onlineUsersCountBadge = [JSCustomBadge customBadgeWithString:#"1"];
CGSize size = onlineUsersCountBadge.frame.size;
onlineUsersCountBadge.frame = CGRectMake(imageViewRect.origin.x+20, imageViewRect.size.height*0.3, size.width, size.height);
[aCustomView addSubview:onlineUsersCountBadge];
This is my view after rotation and added Subview
If I understood it well, you want the view to rotate but the badge to stay normal.
In my opinion, the best approach to this problem is to create an invisible subview containing all the elements (UIViews) you want to rotate (such as the background image). You then shouldn't apply the rotation to the full view, but only to that subview. Doing so, if you want to add an element which shouldn't be rotated, such as your badge, you add it to the main non-rotated view and not to the rotated subview.

only Scrolling programmatically?

I don't know if this makes sense at all. I have UIScrollView from interface builder hooked it up as an outlet on top of my UIScrollView I have a UIImage view which holds an image called Scroll Background it is an extra half in screen real estate - my app is landscape and the image is an about half the screen taller. The image is hooked up as an outlet too. I have a button on a toolbar that brings up the keyboard when the button is pressed I'd like to programmatically scroll to the bottom of the UIImage view and when it is resigned I'd like to programmatically scroll to the top. I don't want the user to be able to scroll just for the app to scroll programmatically.
I can't seem to get this method to work and I'm not sure why :/
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
scrollRectToVisible:animated: is a non-intuative method to use in my opinion. In order to accomplish what you are after you should be able to set userInteractionEnabled to NO on the UIScrollView that will prevent users from scrolling.
Then in order to scroll the view programmatically you can call scrollRectToVisible but you need to give it a CGRect that is representative of the area you want to show. So in your case to scroll to the top:
CGRect visibleFrame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) , CGRectGetHeight(self.view.bounds));
[self.myScrollView scrollRectToVisible:visibleFrame animated:YES];
Hope this helps!

What steps do you need to take to translate UIPanGestureRecognizer touches to ScrollView zooming?

I am creating a gesture recognizer to handle zooming in a scroll view (scrolls along only one on axis at a time). I have the gesture recognizer working but I'm having trouble using the data from the gesture recognizer to transform my views properly. This is what I do right now:
- (void)handlePinch:(GPinchGestureRecognizer *)pinchRecognizer
{
CGSize contentSize = [[self scrollView] contentSize];
if (pinchRecognizer.pinchType == VerticalPinchZoomIn || pinchRecognizer.pinchType == VerticalPinchZoomOut)
{
[[self scrollView] containerView].transform = CGAffineTransformScale([pinchRecognizer transformBeforeTouches], 1, [pinchRecognizer scale]);
[[self scrollView] setContentSize:CGSizeMake(contentSize.width, [pinchRecognizer initialContentSize].height * [pinchRecognizer scale])];
}
...
...
}
This resizes the view nicely, however, it lets it get to strange positions on the screen, and sometimes the scroll view won't be able to scroll all the way to the edge of it (it thinks its boundary is in the middle of the view somewhere and bounces back).
So basically, my question is, what other properties of what other objects and whatnot do I need to operate on to get this working right? View.transform, ScrollView.contentSize, what else? If only Apple's UIScrollView implementation file was open source xD
U do need to add UIPinchGestureRecognizer or neither maintain it.
Just follow this link to add image in scrollview for zooming

Drag a UIView from one UIScrollView to another

I have two UIScrollViews on my screen and I need to be able to drag a UIView from one scrollview to the other.
At the moment, I have a UILongGestureRecognizer on the UIView that I want to move, such that when the user starts dragging it, I make the view follow the touch:
- (void)dragChild:(UILongPressGestureRecognizer *)longPress {
[[longPress view] setCenter:[longPress locationInView:[[longPress view] superview]]];
}
But when I get the boundary of the starting UIScrollView, the view disappears because it's locked into that scrollview's bounds.
Is there a way to "pop" it out of the scrollview when I start dragging such that I can carry it over to the other scrollview?
Also, how do I test for the "drop" location? I want to know if it's been dropped over a certain other view.
Or am I going about this all the wrong way?
Thanks guys
If you will need to drag it from a scroll view to another do the following (pseudo code)
when you start dragging do the following
//scrollView1 is the scroll view that currently contains the view
//Your view is the view you want to move
[scrollView1.superView addSubView:yourView];
now when dropping the view, you will need to know if it is inside the other scrollview
//scrollView2 is the second scroll view that you want to move it too
//Your view is the view you want to move
CGPoint point = yourView.center;
CGRect rect = scrollView2.frame;
if(CGRectContainsPoint(rect, point))
{
//Yes Point is inside add it to the new scroll view
[scrollView2 addSubView:yourView];
}
else
{
//Point is outside, return it to the original view
[scrollView1 addSubView:yourView];
}
Here is an untested idea:
When the drag begins, move the dragging-view out of the scroll view (as a subview) and into the mutual superview of both scroll views.
When the drag ends, move the dragging-view out of the superview and into the new scroll view.
You'll probably have to be careful with coordinate systems, using things like [UIView convertPoint:toView:] to convert between the views' different perspectives when moving things around.

Resources