how to exchange one imageview with another imageview - iphone

i am displaying 3 images on image views.
i am getting image position by using the fallowing code.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSLog(#"%f %f",touchLocation.x,touchLocation.y);
here in console i got the image positions.
Now what i need is when ever imageview is dragged over the imageview2 i need to exchange imageview2 in the place of image view and imageview in the place of imageview2.
can any one please help me.
Thank u in advance.

I wouldn't be replacing the entire imageView2, I would just change the image in imageView2 to the image in imageView.
First, establish that imageView2 is on top of imageView. Second, create an animation block and remove the image from imageView2, change the image in the imageView to the old image from imageView2, and possibly add a nice smooth transition. I would highly recommend a transition animation, it makes the action much more fluid. Use the positions of the imageViews, it wouldn't matter where the image was placed, it would animate as long as it was over top of imageView2.
Remember, there's nothing wrong with faking it. As long as it looks and acts like you want it to, then it works.

Related

Images Move around screen with finger in iOS

I am developing a drawing application for iOS.
I have created a list of images and when user taps on any of the images that will be selected and I also get the tag value of the selected images.
After selection I want to move the images around the screen with finger and I have successfully completed this process, but when I want to apply the tag I will not get the tag value of images with touch. My code for moving the images is below. In this View array is my on screen selected image Array.
CGPoint firstTouch = [touch locationInView:selectedPerson_ImageView];
for (UIImageView *view in viewArray) {
if (CGRectContainsPoint(view.frame, firstTouch))
{
toMove = view;
}
}
How do I get the tag value of images to be moved?
You can get the tag value of touch location view by using this
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
int viewTag=[touch view].tag;
}

How to get effective Bounds of a subview in its parent view's co-ordinate system

In my app, I allow the user to annotate a photo by adding arrows (custom ArrowView). There can be many arrows added, with various zoom & rotation.
I am trying implement selecting of arrow by touch. Currently, I am iterating & using
CGRectContainsPoint(arrowView.frame, touchPoint)
to decide which arrow to select based on a touch gesture.
But, this does not work well when some of the arrows are big & rotated to 45 degrees (since the frame becomes big).
Question:
I would like to use bounds of the arrow translated to parent co-ordinates instead of frame. How can I get this when scaling & rotation is applied?
Alternatively, is there a better method to solve this selection problem?
This code find the arrow under touchPoint:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
UIView *arrow = [self.view hitTest:touchPoint withEvent:event];
}

Xcode Button Click Alpha

Basically I am trying to make a button shaped as a circle and add an image to it. The image is a png with transparency representing a sphere. Adding it to a custom button does it, but it has one problem. The transparent content around the sphere is also clickable. How Do I make so that the nontransparent area of the image is clickable?
You can check out GKTank sample for how to register and use touches.
You have to have to register to get the touch events, then inside the event, check the location, and decide if it is hitting your graphic. To do that, you would have to know the dimmentions and shape of the round button, and then decide if the touch is inside or outside of it.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (myButtonIsActive) {
CGPoint tPoint;
UITouch *thumb = [[event allTouches] anyObject];
tPoint = [thumb locationInView:thumb.view];
// check here if tPoint is inside of the button shape/circle

iPhone : How do I drag or move UIImage/UIButton as shown below?

I dont know How to get following type of functionality in my app.
As shown in above figure user can slide(drag) different image parts and user can assemble image.
Can any one tell me which control is this ? Or any tutorial for it ?
Check out the MoveMe sample app. It will show you how to allow movement of subviews by touch and drag.
You can then create imageViews for each of your puzzle pieces that can be moved around.
Once you have accomplished this you need to check during movement of a piece to see when it gets close enough to it's correct spot in the puzzle so it can snap into place when then user lets go of the piece.
http://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html
That all depends on the kind of implementation you want to do.
It can be UIImageView,UIbutton etc.
The advantage with UIIMageView could be you can easily implement UIGestureRecognizer to move the views..
For an app I built I used the touches functions:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
for dragging UIImageViews around. Then you can use the CGRectContainsRect or CGRectContainsPoint to detect if the puzzle piece is in the right location.
If we talk about UIKit,
this could be a UIImageView with images having partial transparent areas.
My first attack on this problem would be as follows:
(1) subclass UIImageView to hold each piece of the puzzle.
(2) Customize your UIImageView subclass by initializing it with a UIPanGestureRecognizer. Something like:
self.panRecognizer = 
  [[UIPanGestureRecognizer alloc] 
    initWithTarget: self 
    action: #selector(handlePan:)];
[self addGestureRecognizer: 
  self.panRecognizer];
(3) In the action method associated with the pan gesture recognizer, update the object's location based on messages from the puzzle piece's pan gesture recognizer. Something like the following ought to work:
-(void) handlePan: 
  (UIGestureRecognizer *)sender
{
  UIPanGestureRecognizer *panRecognizer = 
    (UIPanGestureRecognizer *)sender;
  if (panRecognizer.state == 
        UIGestureRecognizerStateBegan ||
      panRecognizer.state == 
        UIGestureRecognizerStateChanged)
  {
    CGPoint currentPoint = 
      self.center;
    CGPoint translation = 
      [panRecognizer translationInView: 
        self.superView];
    self.center = CGPointMake
      (currentPoint.x + translation.x, 
        currentPoint.y + translation.y);
    [panRecognizer setTranslation: CGPointZero 
      inView: self.superView];
  }
}
This is not that easy. You need to write the code first for how to move the image views. There are a lot of samples in Apple references. Then you need to get the boundaries of each image to do this.
Here is the code how to move imgTest which is an UIImageView. Tested, work like a charm.
[[self imgTest]setUserInteractionEnabled:YES];
//Save the first touch point
CGPoint firstTouchPoint;
//xd,yd destance between imge center and my touch center
float xd;
float yd;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* bTouch = [touches anyObject];
if ([bTouch.view isEqual:[self imgTest]]) {
firstTouchPoint = [bTouch locationInView:[self view]];
xd = firstTouchPoint.x - [[bTouch view]center].x;
yd = firstTouchPoint.y - [[bTouch view]center].y;
}
}
{
UITouch* mTouch = [touches anyObject];
if (mTouch.view == [self imgTest]) {
CGPoint cp = [mTouch locationInView:[self view]];
[[mTouch view]setCenter:CGPointMake(cp.x-xd, cp.y-yd)];
}
}
source

touch event on a quartz circle drawing?

I have drawn a circle on the iphone simulator using quartz 2d with fill. Is there a way by which I can detect a touch event on that circle?
Thanks
Harikant Jammi
If you have a CGPath of that circle, you can obtain a CGPoint of where the user's finger fell inside of touchesBegan and check to see whether it falls within this CGPath using the following code.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
// Depending on your code, you may need to check a different view than self.view
// You should probably check the docs for the arguments of this function--
// It's been a while since I last used it
if (CGPathContainsPoint(yourCircle, nil, location, nil)) {
// Do something swanky
} else {
// Don't do teh swank
}
}
I haven't tested it, but if the surrounding space around the circle is set to an alpha of 0, then maybe only the circle would accept touches. You'd probably have to turn off isOpaque for the view, and have no background color.
If that doesn't work, then you'll have to process the tap location and write code to see if it is within the circle area or not.
Just put a custom invisible button with 0.0 alpha on that circle, so the button will always be able to detect touch.