UIAttachmentBehavior doesn't work. UIDynamicAnimator - ios

I am having trouble using initWithItem:attachedToItem: Initializes an attachment behavior that connects the center point of a dynamic item to the center point of another dynamic item.
But when I changed the center point of topview using method pan,only the topview moved around,I can't get the other view to move.Isn't it should be moving together?
(BTW I am trying to implement a pile of cards and move around all together when I pan the card in the top.)
-(void)pinch:(UIPinchGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateChanged){
CGPoint pinchCen = [gesture locationInView:self.cardArea];
if (gesture.scale <= 0.5 && !self.pileStat) {
self.pileStat = !self.pileStat;
NSUInteger number = [self.cardViews count];
UIView *topView = [self.cardViews lastObject];
[topView addGestureRecognizer:[[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(pan:)]];
for (int i = 0; i < number;i++) {
UIView *cardView = self.cardViews[i];
[UIView animateWithDuration:0.5 animations:^{cardView.center = CGPointMake(pinchCen.x+i%10*0.5, pinchCen.y+i%10*0.5);} completion:^(BOOL finished){
if(i != number - 1){
UIAttachmentBehavior *attach = [[UIAttachmentBehavior alloc]initWithItem:cardView attachedToItem:topView];
[self.animator addBehavior:attach];
}
}];
}
}
else if(gesture.scale > 1.5 && self.pileStat)
{
self.pileStat = !self.pileStat;
}
}else if (gesture.state == UIGestureRecognizerStateEnded){
gesture.scale = 1.0f;
}
}
-(void)pan:(UIPanGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateChanged || UIGestureRecognizerStateEnded) {
UIView *topView = [self.cardViews lastObject];
CGPoint trans = [gesture translationInView:self.cardArea];
topView.center = CGPointMake(trans.x+topView.center.x, trans.y+topView.center.y);
[gesture setTranslation:CGPointMake(0, 0) inView:self.cardArea];
}
}

setCenter does not play nice with UIDynamicAnimator. Instead of changing the center coordinate of your top view, you should use another UIAttachmentBehavior that you attach to your touch. Replace your pan gesture handler with this method:
//Add a variable in your interface or header section called "touchAttachment" of type UIAttachmentBehavior
- (void) pan: (UIPanGestureRecognizer *) sender{
UIView* topCard = [self.cardViews lastObject];
if (sender.state == UIGestureRecognizerStateBegan){
_touchAttachment = [[UIAttachmentBehavior alloc] initWithItem:topCard
attachedToAnchor: [sender locationInView:self.view]];
[self.animator addBehavior:_touchAttachment];
}
else if (sender.state == UIGestureRecognizerStateChanged){
[_touchAttachment setAnchorPoint: [sender locationInView: self.view]];
}
else if (sender.state == UIGestureRecognizerStateEnded){
[self.animator removeBehavior: _touchAttachment];
_touchAttachment = nil;
}
}
Make sure you add the "touchAttachment" variable.
Hope it helps :)

Related

Cancel multiple touches on the View

I am adding UIPanGestureRecogniser to drag and drop images in a view.I want to drag only one image at a time.but I am able to drag two images at a time.which should not happen.
I am stuck with this bug since morning.I tried all the ways i found on google.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setMultipleTouchEnabled:NO];
for(UIImageView *iView in self.movableArray){
if ([iView isMemberOfClass:[UIImageView class]]){
UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[iView addGestureRecognizer:recognizer];
[iView setUserInteractionEnabled:YES];
recognizer.delegate = self;
}
}
}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
[gameView bringSubviewToFront:[(UIPanGestureRecognizer *)recognizer view]];
CGPoint translation = [recognizer translationInView:gameView];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:gameView];
self.dragObjectImageView = (UIImageView*)recognizer.view;
if(recognizer.state == UIGestureRecognizerStateBegan){
int ind = [self.movableArray indexOfObject:self.dragObjectImageView];
for(int i = 0 ; i < [self.movableArray count] ; i ++){
if( i != ind ){
[[self.movableArray objectAtIndex:i] removeGestureRecognizer:recognizer];
}
}
self.homePosition = self.dragObjectImageView.frame;
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint touchPoint = [recognizer locationInView:gameView];
for (UIImageView *iView in self.staticArray) {
if ([iView isMemberOfClass:[UIImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dropTargetImageView = iView;
}
}
}
if(self.dragObjectImageView.tag == self.dropTargetImageView.tag){
self.dragObjectImageView.frame = CGRectMake(self.dropTargetImageView.frame.origin.x, self.dropTargetImageView.frame.origin.y + self.dropTargetImageView.frame.size.height/2 - 15, self.dragObjectImageView.frame.size.width, self.dragObjectImageView.frame.size.height);
[self.dragObjectImageView removeGestureRecognizer:recognizer];
}
}else{
self.dragObjectImageView.frame = self.homePosition;
}
}
}
This happens because you are adding one UIPanGestureRecognizer for each of your imageViews. Try adding only one to your self.view (and have your imageViews to setUserInteractionEnabled:NO, otherwise they will trap the touch). Also put
recognizer.maximumNumberOfTouches = 1;
Before you add it to your view. All you have to do now is to test which image view should be dragged in your handlePan method. You should check the recognizer state and when it turns to UIGestureRecognizerStateBegan you should save which imageView is being dragged. Then as the state is UIGestureRecognizerStateChanged just drag the view around. The most general way you can find out which view was touched (as i don't know your full view hierarchy) would be to do something like:
NSUInteger index = [self.movableArray indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop)){
UIView* hitTest = (UIView*)obj;
return [hitTest pointInside:firstTouch withEvent:nil];
}];
if ( index != NSNotFound )
self.draggingView = self.moveableArray[index];
else
self.draggingView = nil;
Then of course if self.draggingView is nil you would do nothing when the user is panning around.
You should create a mechanism to setUserInteractionEnable = NO when your
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;
Is called on the other images. You can either do that, or disable the others UIPanGestureRecognizer, like: myPanGestureRecognizer.enabled = NO;
A quick example:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
UIImageView *currentDraggedImageView = recognizer.view;
// Based on this, you can iterate again on your UIImageViews and disable them.
// Once your work is done with gesture recogniser, you can re-enable them.
}

iOS: dragGesture don't move element

In my app I have this code to drag an object
I set my gesture:
UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer new] initWithTarget:self action:#selector(dragGestureChanged:)];
downwardGesture.minimumPressDuration = 0.2;
[grid_element addGestureRecognizer:downwardGesture];
for (UILongPressGestureRecognizer *gestureRecognizer in self.view.gestureRecognizers)
{
[gestureRecognizer requireGestureRecognizerToFail:downwardGesture];
}
and in my method:
- (void) dragGestureChanged:(UILongPressGestureRecognizer*)gesture{
UIImageView *imageToMove;
CGPoint pointInSelfView;
if (gesture.state == UIGestureRecognizerStateBegan)
{
dragging = TRUE;
CGPoint location = [gesture locationInView:grid_element];
NSIndexPath *selectedIndexPath = [grid_element indexPathForItemAtPoint:location];
if (selectedIndexPath==nil) {
dragging = FALSE;
return;
}
indexToHide = selectedIndexPath.row;
imageToMove = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"image_1.png"]];
[self.view addSubview:imageToMove];
pointInSelfView = [gesture locationInView:self.view];
[imageToMove setCenter:pointInSelfView];
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
pointInSelfView = [gesture locationInView:self.view];
[imageToMove setCenter:pointInSelfView];
}
else if (gesture.state == UIGestureRecognizerStateEnded ||
gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed)
{
dragging = FALSE;
}
}
it work in UIGestureRecognizerStateBegan, then imageToMove is added in self.view in the correct position but imageToMove don't drag in UIGestureRecognizerStateChanged; I show a NSLog for pointInSelfView in UIGestureRecognizerStateChanged and it changes correctly; where is the problem?
EDIT
if I use imageToMove as IBOutlet it work fine, but I don't understand the difference.
If you are using ARC, the iOS system is releasing object-imageToMove.
Creating a property with retain attribute, makes system retains the object and hence it moves.
The only thing I am confused is why there is no error in [imageToMove setCenter:pointInSelfView];. There should be an error saying - message is sent to deallocated instance. But I am pretty sure that the problem is due to memory release of imageToMove.

Not able to move two images using uipangesturerecogniser

I want to move two images with same UIPanGestureRecognizer ,
I am able to move the first image, but as soon as I try to move the second image the first one goes back to its original position. I want the first image to retain it's after changed position.
-(void) viewWillAppear:(BOOL)animated
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanSuper:)];
[self.view addGestureRecognizer:pan];
}
- (void)handlePanSuper:(UIPanGestureRecognizer *)sender
{
static UIImageView *viewToMove;
static CGPoint originalCenter;
if (sender.state == UIGestureRecognizerStateBegan)
{
CGPoint location = [sender locationInView:self.view];
if (CGRectContainsPoint(self.imageView.frame, location))
{
viewToMove = imageView;
originalCenter = viewToMove.center;
}
else if (CGRectContainsPoint(self.image2.frame, location))
{
viewToMove = image2;
originalCenter = viewToMove.center;
}
else
{
viewToMove = nil;
}
if (viewToMove)
{
viewToMove.alpha = 0.8;
[viewToMove.superview bringSubviewToFront:viewToMove];
NSLog(#"hi i am being touched.");
}
}
if (sender.state == UIGestureRecognizerStateChanged && viewToMove != nil)
{
CGPoint translation = [sender translationInView:self.view];
viewToMove.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
}
else if ((sender.state == UIGestureRecognizerStateEnded ||
sender.state == UIGestureRecognizerStateFailed ||
sender.state == UIGestureRecognizerStateCancelled) && viewToMove != nil)
{
// do whatever post dragging you want, e.g.
// snap the piece into place
// CGPoint center = viewToMove.center;
//viewToMove.center = center;
viewToMove.alpha = 1.0;
viewToMove = nil;
}
}
You are setting your gesture recognizer in your superView. Why? You could set it inside the views that you are trying to move, it's easier and less likely to strange behaviors I edited your code, see below (not tested)
-(void) viewWillAppear:(BOOL)animated
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanSuper:)];
self.imageView.userInteractionEnabled = self.image2.userInteractionEnabled = YES;
[self.imageView addGestureRecognizer:pan];
[self.image2 addGestureRecognizer:pan];
}
- (void)handlePanSuper:(UIPanGestureRecognizer *)sender
{
UIImageView *viewToMove = (UIImageView*)sender.view;
CGPoint originalCenter = viewToMove.center;
if (sender.state == UIGestureRecognizerStateBegan)
{
CGPoint location = [sender locationInView:self.view];
viewToMove.alpha = 0.8;
[viewToMove.superview bringSubviewToFront:viewToMove];
NSLog(#"hi i am being touched.");
}
else if (sender.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [sender translationInView:self.view];
viewToMove.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
}
else if ((sender.state == UIGestureRecognizerStateEnded ||
sender.state == UIGestureRecognizerStateFailed ||
sender.state == UIGestureRecognizerStateCancelled))
{
// do whatever post dragging you want, e.g.
// snap the piece into place
// CGPoint center = viewToMove.center;
//viewToMove.center = center;
viewToMove.alpha = 1.0;
viewToMove = nil;
}
}
Please try this, and let me know if worked.

Why is my UIPanGestureRecognizer not being detected and firing the method?

I have a UITableView, and I want to attach a UIPanGestureRecognizer to each of the cells, which are subclassed UITableViewCells - ArticleCells. In the awakeFromNib method I add the pan gesture recognizer, but it never fires. Why?
- (void)awakeFromNib {
[super awakeFromNib];
self.cellBack = [[CellBack alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
[self.contentView addSubview:self.cellBack];
self.cellFront = [[CellFront alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
[self.contentView addSubview:self.cellFront];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pannedCell:)];
panGestureRecognizer.delegate = self;
}
Which should be firing this method. But I put a breakpoint on it and it never does get fired.
- (void)pannedCell:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
_firstTouchPoint = [recognizer translationInView:self];
NSLog(#"fired");
}
else if (recognizer.state == UIGestureRecognizerStateChanged) {
NSLog(#"fired");
CGPoint touchPoint = [recognizer translationInView:self];
// Holds the value of how far away from the first touch the finger has moved
CGFloat xPos;
// If the first touch point is left of the current point, it means the user is moving their finger right and the cell must move right
if (_firstTouchPoint.x < touchPoint.x) {
xPos = touchPoint.x - _firstTouchPoint.x;
if (xPos <= 0) {
xPos = 0;
}
}
else {
xPos = -(_firstTouchPoint.x - touchPoint.x);
if (xPos >= 0) {
xPos = 0;
}
}
if (xPos > 10 || xPos < -10) {
// Change our cellFront's origin to the xPos we defined
CGRect frame = self.cellFront.frame;
frame.origin = CGPointMake(xPos, 0);
self.cellFront.frame = frame;
}
}
else if (recognizer.state == UIGestureRecognizerStateEnded) {
[self springBack];
}
else if (recognizer.state == UIGestureRecognizerStateCancelled) {
[self springBack];
}
}
And in the .h file I added so it would be notified as implementing it. But it never calls it as I said.
Why?
Did you do this?
[self.contentView addGestureRecognizer:panGestureRecognizer];

Detecting Touch of a UIView in UIScrollView layer with an array of UIViews

Forgive me I am kinda new at this.
I am trying to detect a touch like the MoveMe example -- only I have an array of UIViews (studentCell) put in to a NSMutableArray called studentCellArray.
[self.studentCellArray addObject:self.studentCell];
When I have one touch I want to make the program smart enough to know if it has touched any of the UIViews in the array and if it has then to do something.
Here is the code in touchesBegan: method.
//prep for tap
int ct = [[touches anyObject] tapCount];
NSLog(#"touchesBegan for ClassRoomViewController tap[%i]", ct);
if (ct == 1) {
CGPoint point = [touch locationInView:[touch view]];
for (UIView *studentCard in self.studentCellArray) {
//if I Touch a Card then I grow card...
}
NSLog(#"I am here[%#]", NSStringFromCGPoint(point));
}
I don't know how t access the views and touch them.
I "solved" this issue by assigning a UIPanGestureRecognizer to each UIView in the array.
This propbably isn't the best way to do it but I can now move them on screen.
Here is the code:
for (int x = 0; x < [keys count]; x++) {
UIPanGestureRecognizer *pGr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(dragging:)];
UIView *sca = [self.studentCellArray objectAtIndex:x];
[sca addGestureRecognizer:pGr];
[pGr release];
}
Here is the "dragging" method I used. I have divided the screen into thirds and there is an animation to snap the UIViews to a point if it happens to cross a threshold. I hope this gives someone some good ideas. Please help if you can see any better way to do this.
- (void) dragging:(UIPanGestureRecognizer *)p{
UIView *v = p.view;
if (p.state == UIGestureRecognizerStateBegan || p.state == UIGestureRecognizerStateChanged) {
CGPoint delta = [p translationInView:studentListScrollView];
CGPoint c = v.center;
c.x += delta.x;
//c.y += delta.x;
v.center = c;
[p setTranslation:CGPointZero inView:studentListScrollView];
}
if (p.state == UIGestureRecognizerStateEnded) {
CGPoint pcenter = v.center;
//CGRect frame = v.frame;
CGRect scrollFrame = studentListScrollView.frame;
CGFloat third = scrollFrame.size.width/3.0;
if (pcenter.x < third) {
pcenter = CGPointMake(third/2.0, pcenter.y);
//pop the view
[self showModalDialog:YES perfMode:YES andControlTag:[studentCellArray indexOfObjectIdenticalTo:p.view]];
}
else if (pcenter.x >= third && pcenter.x < 2.0*third) {
pcenter = CGPointMake(3.0*third/2.0, pcenter.y);
}
else
{
pcenter = CGPointMake(5.0 * third/2.0, pcenter.y);
//pop the view
[self showModalDialog:YES perfMode:YES andControlTag:[studentCellArray indexOfObjectIdenticalTo:p.view]];
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
v.center = pcenter;
[UIView commitAnimations];
}
}
EDIT: Adding [studentCellArray indexOfObjectIdenticalTo:p.view] to the andControlTag gives me the position in the array of the view touched so i can pass that on the my modal dialog to present the appropriate information.

Resources