If I draw a rectangle on a frame and then I want to drag this rectangle to different positions on this frame. How could I do this? Please add comments if my description is unclear. It might be a bit confusing.
You can create a UIView with any background image and move it on a window by setting its frame as yourView.center = CGPointMake(x,y);
You can detect the point of touch using any/all of touchesBegan , touchesMoved or touchesEnded methods as follows :
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[touches anyObject];
CGPoint currentPoint =[touch locationInView:self.view];//point of touch
}
These methods are to be declared in a UIViewController. As sample code, you can refer to my github project in which I drag an item from one UITableView to another UITableView which I've accomplished using UIViews.
Related
I want to scroll the image that is inside my UIImageView in case my finger is inside the UIImageView area and i'm moving my finger. I'm trying to do that using objective c. I got it moving, but it act weird and dont work right. Can you please show me how to do that please???
This is what i'm doing:
- (void)touchesMoved :(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInView:self];
for (id sublayer in view_shape.sublayers) {
if (sublayer ==imageView.layer) {
if (CGRectContainsPoint(imageView.frame, touchLocation)) {
imageView.layer.contentsRect = CGRectMake( touchLocation.x/1000,
touchLocation.y/1000,
imageView.layer.contentsRect.size.width,
imageView.layer.contentsRect.size.height);
}
}
}
}
}
Why not use a scrollView and just add the imageView to it
You can't do this with just an instance of UIImageView. An image view will just hold on to an image and draw that.
Either add that inside a scrollview if you want a simple scroll/paging interface.
(or)
Add that to a UIView, and declare a UIPanGestureRecognizer to the UIView, and check the actions you get. Based on the translation, you can set the frame for the UIImageView.
I have to do some "snap to grid" behaviour,
I have a dragging image "sprite", but I need to "snap" this sprite to a certain button if touches go on top of that button,
so how can i know if touchesEnded is on top of a certain button,
here my code, but i dont know when touches where ended on top of butotn
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint location = [touched locationInView:touched.view];
CGRect recta = touched.view.frame;
if (CGRectIntersectsRect(recta, self.button_1.frame)) {
DLog(#"intersected");
}
}
So this is not happening,
can it be done?
or do i have to check the X and Y position of finishing touch against the button frame X and Y position by my self?
cheers
touched.view is the view on which the touch started. You want to find out if the location where the touch ended is on top of a specific button. You can do this by checking if the location of the touch relative to the button is within the button's bounds.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint locationRelative = [touched locationInView:self.button_1];
if (CGRectContainsPoint(self.button_1.bounds, locationRelative)) {
DLog(#"on top of button_1");
}
}
You can use CGRectIntersectsRect, which returns YES, if the two rectangles your pass to it intersect.
I have a UITableView that has a UIImageView which traverses it one row at a time at the click of a button (up/down). What I would like to do now is allow the user to drag the UIImageView up or down the table ONLY (i.e. no sideways movement). If majority of the UIImageView is over a particular cell, then when the user lets go of their finger, then I want the UIImageView to link to that row. Here is an image of the UITableView, with the UIImageView:
The scroll bar is the UIImageView that needs to move or down. I realize that I am supposed to implement the following methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// We only support single touches, so anyObject retrieves just that touch from touches.
UITouch *touch = [touches anyObject];
if ([touch view] != _imageView) {
return;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == _imageView) {
return;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//here is where I guess I need to determine which row contains majority of the scrollbar. This would only measure the y coordinate value, and not the x, since it will only be moving up or down.
return;
}
}
However, I am not sure HOW to achieve this functionality. I have tried to find similar examples online, and I have looked at the MoveMe example code from Apple, but I am still stuck. Please also note that my scroll bar is NOT the exact same size as a row in the table, but rather, a bit longer, but with the same height.
Thanks in advance to all who reply
Try adding a UIPanGestureRecognizer to the UIImageView. Start by getting the image view's current location, then use the translationInView method to determine where to move the image view.
From Apple's documentation:
If you want to adjust a view's location to keep it under the user's
finger, request the translation in that view's superview's coordinate
system... Apply the translation value to the state of the view when the gesture is first recognized—do not concatenate the value each time the handler is called.
Here's the basic code to add the gesture recognizer:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panView:)];
[imageView addGestureRecognizer:panGesture];
Then, do the math to determine where to move the view.
- (void)panView:(UIPanGestureRecognizer*)sender
{
CGPoint translation = [sender translationInView:self];
// Your code here - change the frame of the image view, and then animate
// it to the closest cell when panning finishes
}
I am doing the View resize,move and rotate on single touch.
I am trying to find the touch moved direction like,
IF touch moved direction is Horizontal or Vertical then MOve the view.
If touch moved direction is diagonal then resize.
And if touch moved like rotate gesture then rotate the View.
I am able to identify the Horizontal or Vertical direction.
Please suggest me how i can identify the diagonal and rotation.
I guess for rotation, you can simply use this mehod -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event.
And for diagonal, you can compare coordinates of both the points.
For diagonal, you can take help from this post also.
Try This:
TDResizerView
both view rotation and move is available in this
in touch move function, you can identify the direction of move finger like this.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint current=[touch locationInView:self];
CGPoint last=[touch previousLocationInView:self];
if(current.x>last.x){
NSLog(#">>>>rigth");
}else{
NSLog(#">>>>left");
}
if(current.y>last.y){
NSLog(#">>>>up");
}else{
NSLog(#">>>>down");
}
}
Just like the title ,when I touch the UIView I want the touch point to be glow, I came up with a idea that to show a glow image on the touch point ,but if by code?any example or idea?thanks for sharing!!
You can try touchesBegan event below Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if([touch view] ==self.my_view)//Check click your uiview or not
{
CGRect frame = [your_image_view frame];
CGPoint touchLocation = [touch locationInView:touch.view];
frame.origin.x=touchLocation.x;
frame.origin.y=touchLocation.y;
[your_image_view setFrame:frame];
[self.my_view addSubview:your_image_view];
}
}
Thanks..!
Well the first step would be to detect the touch on your UIView for that I would recommend using the high level API UIGestureRecognizer andd add to your view. Then you can use the call back to do whatever you want, perhaps some sort of animation?. If so you can use an animation block to animate your changes.