How to move UIimageView over another UIImageView - ios

I am moving most of UIImageViews on view and keeping an UIImageview fixed. Everything is happening fine but problem is when moving imageviews are containing or intersecting the fixed imageview, they are just moving from under fixed imageview.
How to make UIImageviews to move over the fixed imageview.
Please Find the function StartMoving code which makes UIImageviews to move in screen and called on buttonClick event.
In code...
CGPoint pos1,Pos2,pos3;
UIImageView *ball1,*ball2,*ball3,*fixedball;
-(void)StartMoving
{
pos1=CGPointMake(5.0, 5.0);
pos2=CGPointMake(5.0, 8.0);
pos3=CGPointMake(8.0, 5.0);
ball1.center=CGPointMake(ball1.center.x+pos1.x, ball1.center.y+pos1.y);
ball2.center=CGPointMake(ball2.center.x+pos2.x, ball2.center.y+pos2.y);
ball3.center=CGPointMake(ball3.center.x+pos3.x, ball3.center.y+pos3.y);
if(ball1.center.x>320||ball1.center.x<0)
pos1.x=-pos1.x;
if(ball1.center.y>480||ball1.center.y<0)
pos1.y=-pos1.y;
if(ball2.center.x>320||ball2.center.x<0)
pos2.x=-pos2.x;
if(ball2.center.y>480||ball2.center.y<0)
pos2.y=-pos2.y;
if(ball3.center.x>320||ball3.center.x<0)
pos3.x=-pos3.x;
if(ball3.center.y>480||ball3.center.y<0)
pos3.y=-pos3.y;
}
...and have added all moving and fixed imageviews in stroyboard.
Thanks in advance

You need to bring that subview to the front of the other subviews:
UIView *viewContainingBalls; // most likely ball1.superview
[viewContainingBalls bringSubviewToFront:ball1];
Use this method to bring the other balls to the front as needed.
Or if you always want the fixed ball to be behind all the other balls you could just send this to the back:
UIView *viewContainingBalls; // most likely fixedball.superview
[viewContainingBalls sendSubviewToBack:fixedball];
There are other methods such as exchangeSubviewAtIndex:withSubviewAtIndex: that also accomplish similar a thing.

Related

UIScrollView scroll over background view

I have a UIScrollView (with a clear background) and behind it I have a UIImage that takes up about 1/3 of the devices height. In order to initial display the image which is sitting being the scroll view I set the scrollviews contentInset to use the same height as the image. This does exactly what I want, initialing showing the image, but scrolling down will eventually cover the image with the scroll views content.
The only issue is I added a button onto of the image. However it cannot be touched because the UIScrollView is actually over the top of it (even though the button can be seen due to the clear background). How can I get this to work.
Edit:
The following solved the problem:
//viewdidload
self.scrollView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "onScrollViewTapped:"))
...
func onScrollViewTapped(recognizer:UITapGestureRecognizer)
{
var point = recognizer.locationInView(self.view)
if CGRectContainsPoint(self.closeButton.frame, point) {
self.closeButton.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
}
}
Thanks for the screenshots and reference to Google maps doing what you're looking for, I can see what you're talking about now.
I noticed that the image is clickable and is scrolled over but there is no button showing on the image itself. What you can do is put a clear button in your UIScrollView that covers the image in order to make it clickable when you're able to see it. You're not going to be able to click anything under a UIScrollView as far as I can tell.
Please let me know if that works for you.
a simple solution is to reorder the views in the document out line. The higher the view in the outline, the lower the view is as a layer
Two things to test:
1) Make sure the image that contains the button has its userInteractionEnabled set to true (the default is false). Although, since the button is a subview and added on top of the ImageView (I assume) then this might not help.
2) If that doesn't help, can you instead add the button as a subview of the UIScrollView and set its position to be where the image is? This way it should stay on the image and will be hidden as the user scrolls down, but clickable since it is a child of the ScrollView.
Some code and/or images would help as well.
I think the way to do this is to subclass whatever objects are in your UIScrollView and override touches began / touches ended. Then figure out which coordinates are being touched and whether they land within the bounds of your button
e.g. in Swift this would be:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent?) {
println("!!! touchesBegan")
if var touch = touches.first {
var touchObj:UITouch = touch as! UITouch
println("touchesBegan \(touchObj.locationInView(self))") //this locationInView should probably target the main screen view and then test coordinates against your button bounds
}
super.touchesBegan(touches, withEvent:event!)
}
See :
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/index.html#//apple_ref/occ/instm/UIResponder/touchesBegan:withEvent:
And:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITouch_Class/index.html#//apple_ref/occ/instm/UITouch/locationInView:
You should subclass UIScrollView and override -hitTest:withEvent: like so, to make sure it only eats touches it should.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *const inherited = [super hitTest:point withEvent:event];
if (inherited == self) return nil;
return inherited;
}
Also make sure to set userInteractionEnabled to YES in your image view.
There is 2 way you can checked that weather touch event is fire on UIButton or not?
Option 1 : You need to add UITapGesture on UIScrollView. while tapping on UIScrollView. Tap gesture return touch point with respect to UIScrollView. you need to convert that touch point with respect to main UIView(that is self.view) using following method.
CGPoint originInSuperview = [superview convertPoint:CGPointZero fromView:subview];
after successfully conversation, you can checked that weather touch point is interact with UIButton frame or what. if it interact then you can perform you action that you are going to perform on UIButton selector.
CGRectContainsPoint(buttonView.frame, point)
Option 2 : Received first touch event while user touch on iPhone screen. and redirect touch point to current UIViewController. where you can check interact as like in option 1 describe. and perform your action.
Option 2 is already integrated in one of my project successfully but i have forgot the library that received first tap event and redirect to current controller. when i know its name i will remind you.
May this help you.

how to scroll to the center position of the object

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.

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.

Custom UIImageView in UITableViewCell

I created a subclass of UIImageView to pass some strings from and to it. I also created a custom class of UITableViewCell and put four of my UIStringImageView inside of it.
I did this for a gallery like thumbnail tableView.
Then I added an UITapGestureRecognizer to every single UIStringImageView to open the corresponding image.
Now I want to add an overlay onto the UIStringImageView (with opacity) ti indicate touches on the single imageViews.
Here my code (from the UIStringImageView's implementation):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
overlay = [[UIView alloc] initWithFrame:self.frame];
[overlay setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
[self addSubview:overlay];
}
The code works fine. But my problem actually is, that the overlays are not on top of the imageViews, but have got an offset (from top and left). The top offset is the same for every view and the left offset increases per imageView (from left to right).
Screenshot with two overlays (the first and second imageView from left, bottom row):
Can anybody help me with this issue? Thanks a lot! Julian
For everyone running into something like the same problem:
Using self.bounds instead of self.frame solved the issue.

Dragging uiimageview into a "trashcan" while zoomed in on a uiscrollview

Hey everyone. I've searched the web and really haven't found a problem quite like this. My set up is as follows: I am trying to make an app where a user drags uiimageviewss on a uiview inside a uiscrollview (which can zoom in), but also drag the image to a trashcan to be deleted. When the scrollview is zoomed in, the user can still drag because I have subclassed uiimageview and overridden the touchesMoved method. I want the user to be able to drag the uiimageviews to a trashcan in the bottom right corner of the screen. What I implemented already works when the uiscrollview is normal zoom scale, but when you zoom in you cannot drag the image to the trashcan, because well, the trashcan is a subview of the controller's view itself (this was so that it wouldn't zoom with the uiview) and of course is not in the right coordinates of the screen for the uiscrollview.
I've found a view method called ConvertRect that converts a rectangle to the coordinates of whatever view you give to the method, but this doesn't seem to be working. I do a check within each mapimage(the imageview subclass) that checks the frame of the uiimageview intersects the frame of the trashcan. So I try this method before I do that check, but like I said it doesn't seem to be working.
So with all that complicatedness, let me say in a nutshell my problem. I want to drag images over to a trashcan regardless of what zoom level at uiscrollview is at. I can make the trashcan part of the uiscrollview but I need a way to make it keep in the bottom right corner and also not zoom when the uiscrollview does. Any ideas would be so greatly appreciated, thanks guys. :)
I had the exact same challenge.
In can just describe it in short:
When you start dragging the image REMOVE it from the UISCrollView and add it as a subview the the main view of your ViewController.
make sure that the image won't get lost in the process (means: someone still retaining it).
While dragging, constantly check to see if your image entered or left the boundaries of the trash can. you can do it with the method bellow:
-(void) checkForTrashCan
{
CGRect rect = [self.delegate.trashCan convertRect:self.view.frame fromView:self.view.superview];
if ([self.delegate.trashCan rectIntersectBounds:rect])
{
if (NOT self.isInTrashMode)
{
[self enterTrashMode];
}
}
else
{
if (self.isInTrashMode)
{
[self exitTrashMode];
}
}
}
When the method rectIntersectBounds is just a simple extension method for UIView class that i wrote:
-(BOOL) rectIntersectBounds : (CGRect) rect
{
return CGRectIntersectsRect(self.bounds, rect);
}
When the drag ended check if your view is inside the trash can, and act according to the result.

Resources