I have 3 UIViews: needToDragView, its superView, and self.view. When I use touchesMoved: to drag only my needToDragView(lowest in 3Views). But it can drag all 3View. How to detect the lowest UIView?
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UIView *dragView = [[touches anyObject] view];
CGPoint newCenter = [[touches anyObject] locationInView:dragView.superview];
dragView.center = newCenter;
}
Try this:
Make sure you set UserInteractionEnabled = YES; for needToDragView.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
{
UITouch *touch = [touches anyObject];
if ([touch view]== needToDragView) {
// Drag Image Here
}
else {
// NSLog("Wrong");
}
}
UPDATE
Instead of this, you can also use UIPanGestureRecognizer like this:
UIPanGestureRecognizer pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(function:)];
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:2];
[needsToDragView addGestureRecognizer:pan];
[needsToDragView setUserInteractionEnabled:YES];
- (void)function:(UIPanGestureRecognizer *)recognizer
{
NSLog(#"Moving View");
mainView= recognizer.view;
CGPoint translatedPoint = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan)
{
firstX = [mainView center].x;
firstY = [mainView center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[mainView setCenter:translatedPoint];
}
Related
I want my UIViewImages to be movable with touch. I'm trying to use code implemented in my ViewController:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count]==1) {
UITouch *touch = [touches anyObject];
CGPoint p0 = [touch previousLocationInView:self.view];
CGPoint p1 = [touch locationInView:self.view];
CGPoint center = self.view.center;
center.x += p1.x - p0.x;
center.y += p1.y - p0.y;
self.view.center = center;
}
}
When I try to drag an UIImageView, I'm dragging whole screen, which is incorrect.
Need help!
Karol
You create a gesture recognizer and add it to a view like this:
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognizerMethod:)];
[self.imageView addGestureRecognizer:panGestureRecognizer];
you can adjust the position of the image view
- (void)gestureRecognizerMethod:(UIPanGestureRecognizer *)recogniser
{
if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint touchLocation = [recognizer locationInView:self.view];
self.imageView.center = touchLocation;
}
}
read this article.
If I read your code right self.view actually IS the whole screen.
Maybe you mean self.yourImageView instead?
I am using - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event to move an image.How I can fix its position after moving it.I mean I would like when I choose the right position,the image should not move no more.
Add PanGesture to the imageView containing the image.
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
[imageView addGestureRecognizer:panRecognizer];
//method to move imageView whereever you want in the view,you can also write conditions to restrict the imageView not to go beyond bounds.
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.view];
CGPoint imageViewPosition = imageView.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;
imageView.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView:self.view];
//you can use if(panRecognizer.state==UIGestureRecognizerStateEnded){} condition to fix the position of the imageView.
}
you can do like this:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIImageView *view1 =moveImageView;
CGPoint point = [touch locationInView:self.view];
//the right position
if(point.x==?&&point.y==?){
rightPosition = YES;
moveImageView.frame = CGRectMake(point.x, point.y, 50, 50);
}else{
//the UIImageView you want to move
if(!rightPosition){
moveImageView.frame = CGRectMake(point.x, point.y, 50, 50);
}
}
}
i have an image view as background image.
I am seeking that in some place on image view touches would be enabled.
I started with this:
- (id)initWithTouchPoint:(CGRect )point
{
self = [super init];
if (self) {
touchFrame = point;
[self setAccessibilityFrame:touchFrame];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(BOOL)canResignFirstResponder{
return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(touchFrame, touchLocation)) {
//[self setUserInteractionEnabled:NO];
}else{
//[self setUserInteractionEnabled:YES];
}
DLog(#"touchesBegan at x : %f y : %f",touchLocation.x,touchLocation.y);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
Is it possible to let user touch over image view when user touches in touchFrame ?
Thank you.
Add UITapGestureRecognizer on UIImageView
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleGesture:)];
[gesture setNumberOfTapsRequired:1];
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:gesture];
Now within the HandleGesture Method :
-(void)handleGesture:(UITapGestureRecognizer *)_gesture
{
if (_gesture.state == UIGestureRecognizerStateEnded)
{
CGPoint touchedPoint = [_gesture locationInView:self.view];
}
}
you can check now wether the touchedPoint within the handleGesture method are in the specified area or not and you can perform your desired task accordingly
You can try having a boolean variable as a class member say BOOL allowTouch initialised with value NO:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(touchFrame, touchLocation)) {
allowTouch = YES;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if(allowTouch)
{
//handle moves here
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
allowTouch = NO;//you can put your condition too to end touches
}
It may help.
I have implemented the drag effect on an image but during my test I see that the image is moving only on the click mouse event.
I cannot move my image with the mouse on my screen through the drag event. But when I click on a side of my screen the image take the place where I have clicked.
I followed many topics on youtube but finally, I haven't the same behavior.
This my code:
ScreenView1.h
IBOutlet UIImageView *image;
ScreenView1.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
image.center = location;
[self ifCollision];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:touches withEvent:event];
}
If you want to drag an image view, you will be so much happier using a UIPanGestureRecognizer. It makes this sort of thing trivial. Using touchesBegan is so iOS 4!
UIPanGestureRecognizer* p =
[[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(dragging:)];
[imageView addGestureRecognizer:p];
// ...
- (void) dragging: (UIPanGestureRecognizer*) p {
UIView* vv = p.view;
if (p.state == UIGestureRecognizerStateBegan ||
p.state == UIGestureRecognizerStateChanged) {
CGPoint delta = [p translationInView: vv.superview];
CGPoint c = vv.center;
c.x += delta.x; c.y += delta.y;
vv.center = c;
[p setTranslation: CGPointZero inView: vv.superview];
}
}
You're not doing the right thing in the touchesMoved:withEvent:, which is why the drag won't work. Here's a little code that works:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
[CATransaction begin];
[CATransaction setDisableActions:YES];
[image setCenter:location];
[CATransaction commit];
}
For the others, I have implemented my issue in that way:
- (IBAction)catchPanEvent:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
thank you again Matt!
I have a problem, I want to drag a label with finger, and I can't even register a touch on it. What can be a problem? self is my viewcontroller and self.label is my label.
EDIT:
My label is made programmatically over an uiimageview. UserInteractionEnables is set to YES.
I am not sure what is wrong, here is code in its entirety:
Init:
UIImage *myImage = [UIImage imageNamed:#"fish.jpg"];
UIImageView *mainImageView = [[UIImageView alloc] initWithImage:myImage];
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view = mainImageView;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
self.label.userInteractionEnabled = YES;
self.label.text = #"Hello, World";
[self.view addSubview:self.label];
Dragging:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([[touch view] isEqual:self.label])
{
NSLog(#"touch on label");
self.startLocation = [[touches anyObject] locationInView:self.label];
// startLocation is a CGPoint declare globaly in .h..and lblName is your UILabel
}
}
- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == self.label)
{
CGPoint pt = [[touches anyObject] previousLocationInView:self.label];
CGFloat dx = pt.x - self.startLocation.x;
CGFloat dy = pt.y - self.startLocation.y;
CGPoint newCenter = CGPointMake(self.label.center.x + dx, self.label.center.y + dy);
self.label.center = newCenter;
}
}
EDIT 2:
I have also tried this code, but it doesn't work too. I think the problem is not in these methods but with registering touches.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.startLocation = [[touches anyObject] locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self.label];
self.label.center = CGPointMake(self.label.center.x + point.x - self.startLocation.x, self.label.center.y + point.y - self.startLocation.y);
}
Make sure you check "User Interaction Enabled" on the label in interface builder, or if you you doing it in code, userInteractionEnabled=YES
By default, UILAbel does not respond to touches. You need to enable the userInteractionEnabled property.
label.userInteractionEnabled = YES;