Dragging a UIView created after touchesBegan - objective-c

I have a few UIView subclasses that exist in a kind of inventory. When you tap one, another draggable version is placed over top of it. I want the inventory versions to stay put while you drag others around. The code to make a duplicate works but dragging your finger around doesn't move it.
If I release and then start dragging the newly created version it moves as expected. I think this is because the original touches (that made the dupe) didn't have the draggable version in the responder chain.
A little code...
in my stationary "icon"...
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
[self.viewController placeDraggableItem:self.item WithPoint:self.frame.origin];
}
in my viewController...
- (void)placeDraggableItem:(Item *)item WithPoint:(CGPoint)point
{
DraggableItem *draggableItem = [[DraggableItem alloc] initWithImage:[UIImage imageNamed:item.graphic]];
draggableItem.frame = CGRectMake(point.x, scrollView.frame.origin.y + point.y, 64.0f, 64.0f);
[self.view addSubview:draggableItem];
[draggableItem release];
}
in my DraggableItem...
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x), self.center.y + (activePoint.y - currentPoint.y));
float midPointX = CGRectGetMidX(self.bounds);
if (newPoint.x > self.superview.bounds.size.width - midPointX)
newPoint.x = self.superview.bounds.size.width - midPointX;
else if (newPoint.x < midPointX) // If too far left...
newPoint.x = midPointX;
float midPointY = CGRectGetMidY(self.bounds);
if (newPoint.y > self.superview.bounds.size.height - midPointY)
newPoint.y = self.superview.bounds.size.height - midPointY;
else if (newPoint.y < midPointY) // If too far up...
newPoint.y = midPointY;
self.center = newPoint;
}
Now again creating the draggable version works. And the draggable version is able to be moved after you first release your first touch. But I think I need to get the newly create UIView to respond to touches that were originally made for the "icon".
Any ideas?
I'm aware this question is kind of similar to this one: How to "transfer" first responder from one UIView to another? but in that case the view which should receive the touches is already there whereas I need to pass touches onto a newly created view.

I don't know of any way to hand touches off to a newly created view, though that doesn't mean there isn't a way to do it. It looks like you really only want your draggable view to handle the touches, I would probably create each draggable view with with an alpha of 0 at the same time you create the non draggable views, and in touchesBegan set it to 1. If your non-draggable views don't need to handle touches, then it doesn't make sense to make them handle touches just to pass them along.

Related

When showing the real time resizing of a pane, it struggles while dragging and top of the pane is hidden after released

I went through several posts on dragging but couldn't find an answer to my problem.
I can use the mouseDown and mouseUp events to track the current positions and redraw the resized pane. What I want is to show the real time movement of the pane. Everytime mouseDragged event is fired, y coordinate of the new location is taken and setFrame is called to redraw. The window seems to flicker and gets stuck finally (title bar goes out of bounds and hidden) as it seems to miss the final events in the run loop.
Is there a way to solve this problem?
The view has been implemented in the following way
NSSplitView (divided into sections left dock, right dock, etc.)
NSView is used to implement a sub view inside the dock
NSTableView is used inside the NSView to hold multiple "panels"
There can be several panels inside this table view (one below another)
I need to resize these panels by dragging the border line. For this I'm using an NSButton in the bottom.(I want to show a thicker separation line there)
Here is the code for mouseDown, mouseUp, mouseDragged callbacks, used to resize the panel
-(void)mouseDown:(NSEvent *)theEvent {
draggingInProgress = YES;
firstDraggingPointFound = NO;
}
-(void)mouseUp:(NSEvent *)theEvent {
if (!draggingInProgress) {
return;
}
NSPoint point = [self convertPoint: [theEvent locationInWindow] fromView: nil];
if (firstDraggingPointFound) {
[_delegate heightChanged:[NSNumber numberWithFloat:(point.y - previousDragPosition)]];
}
draggingInProgress = NO;
[_delegate heightChangingEnded]; //draggingInProgress is set to NO
}
-(void)mouseDragged:(NSEvent *)theEvent {
if (!draggingInProgress) {
return;
}
NSPoint point = [self convertPoint: [theEvent locationInWindow] fromView: nil];
if (firstDraggingPointFound) {
[_delegate heightChanged:[NSNumber numberWithFloat:(point.y - previousDragPosition)]];
} else {
firstDraggingPointFound = YES;
}
previousDragPosition = point.y;
}
//Delegate method
-(void)heightChanged:(NSNumber *)change {
NSRect f = [[self view] frame];
f.size.height += [change floatValue];
if (f.size.height < 100) {
f.size.height = 100;
}
[[self view] setFrame:f];
[self.panelViewModel setPanelHeight:f.size.height];
if (_heightChangeDelegate) {
[_heightChangeDelegate heightChangedForPanel:self];
}
[[self view] setFrame:f];
}
What would be the problem here?
Is there a better way to do this?
First off, I wouldn’t use the word “Panel” to describe what you want, since an “NSPanel” is a kind of “NSWindow”. Let’s call them “panes.”
Second, I wouldn’t use an NSTableView to contain your panes. NSTableView really isn’t designed for that, it’s for tabular data. You can use an NSStackView, which IS designed for it, or just use raw constraints and autolayout, manually setting the top of each pane to equal the bottom of the previous one.

Problems with UIPanGestureRecogniser

Working with XCode 4.2 and trying to get to grips with the UIGestureRecognisers. All seems to be going fairly well so far, but am still having a few issues.
When I was using the Swipe gesture recognisers, everything was fine, it would recognise swipes in all different directions and would do so continuously. My problem now is, when using the pan gesture recogniser, it recognises the first pan swipe fine, but then just refuses to accept any further gestures. So I can move items about once as needed, but after that, can do nothing.
I set my gesture up as follows:
UIPanGestureRecognizer *panBody = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panBody:)];
[bodyGestureView addGestureRecognizer:panBody];
Then this is my 'panBody' method that handles it all:
- (void)panBody:(UIPanGestureRecognizer *)recognizer
{
CGPoint translate = [recognizer translationInView:self.view];
CGRect bodyPanelFrame = bodyPanel.frame;
bodyPanelFrame.origin.x += translate.x;
bodyPanelFrame.origin.y += translate.y;
recognizer.view.frame = bodyPanelFrame;
CGRect topPanelFrame = topPanel.frame;
topPanelFrame.origin.x += translate.x;
topPanelFrame.origin.y += translate.y;
recognizer.view.frame = topPanelFrame;
CGRect sidePanelFrame = sidePanel.frame;
sidePanelFrame.origin.x += translate.x;
sidePanelFrame.origin.y += translate.y;
recognizer.view.frame = sidePanelFrame;
NSLog(#"Panning");
if (recognizer.state == UIGestureRecognizerStateEnded)
{
bodyPanel.frame = bodyPanelFrame;
if((topPanel.frame.origin.x + translate.x) <= 193)
{
topPanel.frame = CGRectMake(topPanelFrame.origin.x, topPanel.frame.origin.y, topPanel.frame.size.width, topPanel.frame.size.height);
}
else
{
topPanel.frame = CGRectMake(193, 0, topPanel.frame.size.width, topPanel.frame.size.height);
NSLog(#"Top panel not in frame");
}
if((sidePanel.frame.origin.y + translate.y) < 57)
{
sidePanel.frame = CGRectMake(sidePanel.frame.origin.x, sidePanelFrame.origin.y, sidePanel.frame.size.width, sidePanel.frame.size.height);
}
else
{
sidePanel.frame = CGRectMake(0, 56, sidePanel.frame.size.width, sidePanel.frame.size.height);
NSLog(#"Side panel not in frame");
}
}
}
bodyPanel, topPanel and sidePanel are IBOutlets linked to UIView's overlayed across the top of my interface .xib
If anybody could shed any light on this information, that would be great cause I have absolutely no idea what is going on!!
Thanks,
Matt
First I would check that
if (recognizer.state == UIGestureRecognizerStateChanged)
before doing your translations (there are many other possible states which would not justify you taking any action). Also I would reset the translation at every callback given you are accumulating them using the UIPanGestureRecognizer method
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
If the gesture recognizer stops it might be that another gesture recognizer is interfering with it. Do you still have an active UISwipeGestureRecognizer there? If so you should probably deactivate one of them. You can also look at this method
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer
which allows you to specify which recognizer should be given priority.

How do I pass the user touch from one object to another object?

I am developing an application that allows the user at a certain point to drag and drop 10 images around. So there are 10 images, and if he/she drags one image onto another, these two are swapped.
A screenshot of how this looks like:
So when the user drags one photo I want it to reduce its opacity and give the user a draggable image on his finger which disappears again if he drops it outside of any image.
The way I have developed this is the following. I have set a UIPanGesture for these UIImageViews as:
for (UIImageView *imgView in editPhotosView.subviews) {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(photoDragged:)];
[imgView addGestureRecognizer:panGesture];
imgView.userInteractionEnabled = YES;
[panGesture release];
}
Then my photoDragged: method:
- (void)photoDragged:(UIPanGestureRecognizer *)gesture {
UIView *view = gesture.view;
if ([view isKindOfClass:[UIImageView class]]) {
UIImageView *imgView = (UIImageView *)view;
if (gesture.state == UIGestureRecognizerStateBegan) {
imgView.alpha = 0.5;
UIImageView *newView = [[UIImageView alloc] initWithFrame:imgView.frame];
newView.image = imgView.image;
newView.tag = imgView.tag;
newView.backgroundColor = imgView.backgroundColor;
newView.gestureRecognizers = imgView.gestureRecognizers;
[editPhotosView addSubview:newView];
[newView release];
}
else if (gesture.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [gesture translationInView:view.superview];
[view setCenter:CGPointMake(view.center.x + translation.x, view.center.y + translation.y)];
[gesture setTranslation:CGPointZero inView:view.superview];
}
else { ... }
}
}
Thus as you see I add a new UIImageView with 0.5 opacity on the same spot as the original image when the user starts dragging it. So the user is dragging the original image around. But what I want to do is to copy the original image when the user drags it and create a "draggable" image and pass that to the user to drag around.
But to do that I have to pass the user touch on to the newly created "draggable" UIImageView. While it's actually set to the original image (the one the user touches when he starts dragging).
So my question is: How do I pass the user's touch to another element?.
I hope that makes sense. Thanks for your help!
Well, you can pass the UIPanGestureRecognizer object to another object by creating a method in your other object which takes the gesture recognizer as a parameter.
- (void)myMethod:(UIPanGestureRecognizer *)gesture
{
// Do stuff
}
And call from your current gesture recognizer using....
[myOtherObject myMethod:gesture];
Not entirely sure I'm understanding your question here fully. :-/
Maybe:
[otherObject sendActionsForControlEvents:UIControlEventTouchUpInside];
Or any other UIControlEvent
In the end I decided to indeed drag the original image and leave a copy at the original place.
I solved the issue with the gesture recognizers I was having by re-creating them and assigning them to the "copy", just like PragmaOnce suggested.

Presenting a view by pulling from the top of the view

So I am planning to do a different way to display history in my iPad app and I think user could pull up the history view from the bottom, should I just place UIView there and add gesture recognizer for it? Is there any "right" way to do this? I want to make the user actually able to drag the view from the bottom. If you don't understand, ask away and I will elaborate.
You have the right idea. You'd use a UIPanGestureRecognizer to update the view's frame. Keep in mind that you'll have to have something for the user to "pull" on visible at all times - I don't think you'll be able to hide the view completely off screen.
Something like this would go in the implementation of the object you choose to handle events from your gesture recognizer (this sample assumes it's your view controller):
- (void)handleDrag:(UIPanGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateChanged ||
gesture.state == UIGestureRecognizerStateEnded) {
CGPoint translation = [gesture translationInView:self.view];
CGRect newFrame = historyView.frame;
newFrame.origin.x = newFrame.origin.x + translation.x;
newFrame.origin.y = newFrame.origin.y + translation.y;
historyView.frame = newFrame;
// you need to reset this to zero each time
// or the effect stacks and that's not what you want
[gesture setTranslation:CGPointZero inView:self.view];
}
}

Control the animation by touch

i have this type of code....
// create a UIImageView
UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"rollDiceAnimationImage1.png"]];
// position and size the UIImageView
rollDiceImageMainTemp.frame = CGRectMake(0, 0, 100, 100);
// create an array of images that will represent your animation (in this case the array contains 2 images but you will want more)
NSArray *savingHighScoreAnimationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"rollDiceAnimationImage1.png"],
[UIImage imageNamed:#"rollDiceAnimationImage2.png"],
nil];
// set the new UIImageView to a property in your view controller
self.viewController.rollDiceImage = rollDiceImageMainTemp;
// release the UIImageView that you created with alloc and init to avoid memory leak
[rollDiceImageMainTemp release];
// set the animation images and duration, and repeat count on your UIImageView
[self.viewController.rollDiceImageMain setAnimationImages:savingHighScoreAnimationImages];
[self.viewController.rollDiceImageMain setAnimationDuration:2.0];
[self.viewController.rollDiceImageMain.animationRepeatCount:3];
// start the animation
[self.viewController.rollDiceImageMain startAnimating];
// show the new UIImageView
[self.viewController.view addSubview:self.rollDiceImageMain];
Instead of startAnimation directly..there any way to control this code using touchesMoved??
Actually, what you want is the - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event. Below is a sample of code that will allow a UIView to move on the x-axis only. Adapt to whatever you need your code to do.
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint original = self.center;
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
// Taking the delta will give us a nice, smooth movement that will
// track with the touch.
float delta = location.x - original.x;
// We need to substract half of the width of the view
// because we are using the view's center to reposition it.
float maxPos = self.superview.bounds.size.width
- (self.frame.size.width * 0.5f);
float minPos = self.frame.size.width * 0.5f;
float intendedPos = delta + original.x;
// Make sure they can't move the view off-screen
if (intendedPos > maxPos)
{
intendedPos = maxPos;
}
// Make sure they can't move the view off-screen
if (intendedPos < minPos)
{
intendedPos = minPos;
}
self.center = CGPointMake(intendedPos, original.y);
// We want to cancel all other touches for the view
// because we don't want the touchInside event firing.
[self touchesCancelled:touches withEvent:event];
// Pass on the touches to the super
[super touchesMoved:touches withEvent:event];
}
Notice here that I am taking the delta of the movement and not with the finger. If you track with the finger you will get very erratic behavior that is very undesirable. Applying the delta will give nice, fluid movement of the view that will track perfectly with the touch input.
Update: Also, for those wondering why I chose to multiply by 0.5f rather than divide by 2, the ARM processor doesn't support division in hardware, so there is a minuscule performance bump by going with multiplication. This performance optimization may only get called only a few times during the life of a program so it might not make a difference, but this particular message is called many, many times when dragging. Because it is in this case, it might be worth the multiplication, instead.
You can detect touches by using UIResponder methods like - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event in that you can call the [self.viewController.rollDiceImageMain startAnimating]; method.
And once it starts then you can stop after some time and restrict the touches.