I want this item to Pop up briefly before being pulled down by gravity. Right now if I add the push effect it launches down.
//Animate this new fake button
UIGravityBehavior* gravity = [[UIGravityBehavior alloc] initWithItems:#[fakeButton]];
[gravity setMagnitude:1.0f];
UIPushBehavior *upFlick = [[UIPushBehavior alloc]initWithItems:#[fakeButton] mode:UIPushBehaviorModeInstantaneous];
[upFlick setAngle:M_PI/3 magnitude:1.0f];
[self.animator addBehavior:gravity];
[self.animator addBehavior:upFlick];
You should make either the angle or the magnitude negative, and that should make it go up (and to the side). If you want it to go straight up, you should also change the angle to M_PI/2.
Related
I want to perform a two finger swipe in my UI Test. I am using XCUITest framework. I tried all pinch and rotate methods but Seems like there is no built in support for this functoinality.
I don't know if it will work, but you can try to simulate two different fingers dragging screen simultaneously. Something like this:
XCUIApplication *app = [[XCUIApplication alloc] init];
[app launch];
// Set a coordinate near the left-edge, we have to use normalized coords
// so you set using percentages, 1% in on the left, 15% down from the top
XCUICoordinate *coord1 = [app coordinateWithNormalizedOffset:CGVectorMake(0.01, 0.15)];
// Then second coordinate 40 points to the right
XCUICoordinate *coord2 = [coord1 coordinateWithOffset:CGVectorMake(40, 0)];
// Third coordinate 100 points down from the first
XCUICoordinate *coord3 = [coord1 coordinateWithOffset:CGVectorMake(0, 100)];
// Last one is 100 points down from the second
XCUICoordinate *coord4 = [coord2 coordinateWithOffset:CGVectorMake(0, 100)];
// Perform a drag from coord1 to coord3
[coord1 pressForDuration:0.5f thenDragToCoordinate:coord3];
// Perform a drag from coord2 to coord4
[coord2 pressForDuration:0.5f thenDragToCoordinate:coord4];
I am a beginner to iOS app coding but not to coding in general. I am following a tutorial for a sprite game kit app and I am having some trouble with the sizing of stuff. It came time to put in edges so the balls can bounce off of them. I noticed that the edges seemed to be off the screen, the balls would leave the screen then bounce back.
So I had xcode output the width/height, it said 320x568. I also change the scale mode by setting scene.scaleMode = SKSceneScaleModeAspectFit; and you can see the boundaries look like a square:
If I put the scaling back to normal by scene.scaleMode = SKSceneScaleModeAspectFill; It shows the screen like this:
but the balls still go off the screen.
By the way, I believe I am setting the edge nodes correctly to be on the left and right edge so I don't think that is the issue.
// Add edges
SKNode *leftEdge = [[SKNode alloc] init];
leftEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, self.size.height)];
leftEdge.position = CGPointMake(0, 0);
[self addChild:leftEdge];
SKNode *rightEdge = [[SKNode alloc] init];
rightEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, self.size.height)];
rightEdge.position = CGPointMake(self.size.width, 0);
[self addChild:rightEdge];
In your GameScene file (or whatever you may have called the SKScene file), add this method to the start of didMoveToView:
self.size = self.view.frame.size;
What is happening is that your game thinks the screen is running a different size. You need to change self.size to the actual size of the frame you are working with. The above method works with SKSceneScaleModeAspectFill, but I have not yet tested it with the other modes. Good luck!
I have two UIViews.i want to set different gravitydirection vector for these two views for making **attraction**.and i am doing this by having two gravitybehavior but when i am adding these two to animator.
it's not working as **attracting** to each other.
also giving warning like :-Multiple gravity behavior per animator is undefined and may assert in the future.
Below is the code i am using to make Attractiuon effect b/w two views.
gravity behavior is to make the gravity animation.and animator is dynamic animator.
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:#[v1]];
CGVector vector = CGVectorMake(1.0, 1.0);
[gravityBehavior setGravityDirection:vector];
UIGravityBehavior *gravityBehavior1 = [[UIGravityBehavior alloc] initWithItems:#[v2]];
CGVector vector1 = CGVectorMake(-1.0, -1.0);
[gravityBehavior1 setGravityDirection:vector1];
gravityBehavior.magnitude=1000;
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
[self.animator addBehavior:gravityBehavior];
[self.animator addBehavior:gravityBehavior1];
Please Help!
Thanks for the Help!!
There are many situations where you need to "shake" a UIView.
(For example, "draw child user's attention to a control", "connection is slow", "user enters bad input," and so on.)
Would it be possible to do this using UIKit Dynamics?
So you'd have to ..
take the view, say at 0,0
add a spring concept
give it a nudge, say to the "left"
it should swing back and fore on the spring, ultimately settling again to 0,0
Is this possible? I couldn't find an example in the Apple demos. Cheers
Please note that as Niels astutely explains below, a spring is not necessarily the "physics feel" you want for some of these situations: in other situations it may be perfect. As far as I know, all physics in iOS's own apps (eg Messages etc) now uses UIKit Dynamics, so for me it's worth having a handle on "UIView bouncing on a spring".
Just to be clear, of course you can do something "similar", just with an animation. Example...
But that simply doesn't have the same "physics feel" as the rest of iOS, now.
-(void)userInputErrorShake
{
[CATransaction begin];
CAKeyframeAnimation * anim =
[CAKeyframeAnimation animationWithKeyPath:#"transform"];
anim.values = #[
[NSValue valueWithCATransform3D:
CATransform3DMakeTranslation(-4.0f, 0.0f, 0.0f) ],
[NSValue valueWithCATransform3D:
CATransform3DMakeTranslation(4.0f, 0.0f, 0.0f) ]
];
anim.autoreverses = YES;
anim.repeatCount = 1.0f;
anim.duration = 0.1f;
[CATransaction setCompletionBlock:^{}];
[self.layer addAnimation:anim forKey:nil];
[CATransaction commit];
}
If you want to use UIKit Dynamics, you can:
First, define a governing animator:
#property (nonatomic, strong) UIDynamicAnimator *animator;
And instantiate it:
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
Second, add attachment behavior to that animator for view in current location. This will make it spring back when the push is done. You'll have to play around with damping and frequency values.
UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:viewToShake attachedToAnchor:viewToShake.center];
attachment.damping = 0.5;
attachment.frequency = 5.0;
[self.animator addBehavior:attachment];
These values aren't quite right, but perhaps it's a starting point in your experimentation.
Apply push behavior (UIPushBehaviorModeInstantaneous) to perturb it. The attachment behavior will then result in its springing back.
UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:#[viewToShake] mode:UIPushBehaviorModeInstantaneous];
push.pushDirection = CGVectorMake(100, 0);
[self.animator addBehavior:push];
Personally, I'm not crazy about this particular animation (the damped curve doesn't feel quite right to me). I'd be inclined use block based animation to move it one direction (with UIViewAnimationOptionCurveEaseOut), upon completion initiate another to move it in the opposite direction (with UIViewAnimationOptionCurveEaseInOut), and then upon completion of that, use the usingSpringWithDamping rendition of animateWithDuration to move it back to its original spot. IMHO, that yields a curve that feels more like "shake if wrong" experience.
I'd like to make it when the user shakes the device the ball rattles around inside the object on screen. I'm assuming I need to set up an invisible box for it to collide with. It doesn't matter if it moved randomly or follows a predefined path, whichever is easiest.
I think I understand the "Activate on shake" part of the code, just the ball/object movement I'm not sure of
This should work:
//You need an #property (nonatomic, strong) UIDynamicAnimator *animator; in your .h
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.viewToBounceAroundIn];
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:#[self.viewThatBouncesAround]];
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collision];
UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:#[self.viewThatBouncesAround] mode:UIPushBehaviorModeInstantaneous];
push.magnitude = 1; //Play with this, it's how much force is applied to your object
push.angle = 0; //play with this too
[self.animator addBehavior:push];
I typed this away from a compiler - let me know if it works. The idea is that you use UIKitDynamics as a physics engine, use a UICollisionBehavior to let the item bounce around inside the box, and a UIPushBehavior to apply the initial force.
If the item slows down too quickly for you, or loses too much energy when it bounces off walls, you can adjust its properties:
UIDynamicItemBehavior *behavior = [[UIDynamicItemBehavior alloc] initWithItems:#[self.itemThatBouncesAround]];
behavior.friction = 0; //no friction. play with this.
behavior.elasticity = 1;; //completely elastic, play with this.
[self.animator addBehavior:behavior];