Swift animation doesn't update during while loop - swift

I am new to Swift. I am trying to animate an object within a while loop, but the screen doesn't update until after the while loop is completed. If I run the function that the while loop is in by repeatedly clicking a button, it does work, but I want it run on its own. I have tried loading the screen every loop, but it didn't work.
func moveBall() {
while loopnumb <= 100 {
loopnumb += 1
print("animation loop cycled \(loopnumb) times")
print("inside of moveball() before startanimation, scratchX is \(scratchX), scratchY is \(scratchY), and the direction is \(direction).")
let animator = UIViewPropertyAnimator(duration: 0.5, curve: .linear) {
self.direckEffeck()
self.scratch.frame = CGRect(x: self.scratchX, y: self.scratchY, width: 240, height: 128)
print("after and inside of animation command, scratchX is \(self.scratchX), scratchY is \(self.scratchY), and the direction is \(self.direction).")
}
animator.startAnimation()
if checkForOOBX() == true && checkForOOBY() == false {
direckCheckX()
}
if checkForOOBY() == true && checkForOOBX() == false {
direckCheckY()
}
if checkForOOBX() == true && checkForOOBY() == true {
if direction == 0 && done == false {
direction = 2
done = true
}
if direction == 1 && done == false {
direction = 3
done = true
}
if direction == 2 && done == false {
direction = 0
done = true
}
if direction == 3 && done == false {
direction = 1
done = true
}
cornerTouches += 1
print("ebic win, gamer's! the amount of corner touches is \(cornerTouches)")
}
done = false
DispatchQueue.main.async() {
self.scratch.setNeedsDisplay()
}
}
loopnumb = 0
}
Note that the object I am trying to move is called "scratch", and its coordinates are scratchX, scratchY. Thank you in advance.

Related

How to make GetTouch(1) not depending on GetTouch(0) and touchCount

I'm making an app which asks to keep touch 2 things at same time, but sometimes only one.
So my problem is: GetTouch(1) depends on GetTouch(0) and touchCount.
If I release 1 time the getTouch(0) : getTouch(1) will not be called anymore.
I don't know if I'm clear so here is my code:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
}
if (touch.phase == TouchPhase.Ended)
{
}
if (Input.touchCount == 2)
{
Touch touch_ = Input.GetTouch(1);
if (touch_.phase == TouchPhase.Began)
{
}
if (touch_.phase == TouchPhase.Ended)
{
}
}
}
How can I do to still keep the Touch(1) even without Touch(0) Pressed please ? Just keep doing my stuff during "touch_.phase == TouchPhase.Began".
Thank you in advance.
you will only be able to access Input.GetTouch(1) when the touchCount is 2 or greater. You are probably looking for some sort of state variable
bool firstPress = false;
bool secondPress = false;
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
firstPress = true;
}
if (touch.phase == TouchPhase.Ended)
{
firstPress = false;
}
if (Input.touchCount == 2)
{
Touch touch_ = Input.GetTouch(1);
if (touch_.phase == TouchPhase.Began)
{
secondPress = true;
}
if (touch_.phase == TouchPhase.Ended)
{
secondPress = false;
}
}
}
if (firstPress == true && secondPress == false)
{
...
}
if (firstPress == true && secondPress == true)
{
...
}
While the GetTouch has no guarantee that the same index returns the same touch in the next frame what you can use use the Touch.fingerIndex which is unique for each touch.
So instead of using GetTouch you can access the touches via Input.touches and then try to filter out the one for your stored fingerId e.g. like
foreach (Touch touch in Input.touches)
{
if (touch.fingerIndex == storedFingerIndex)
{
// Do something
}
}

touchCount difficulty on Android

This could be because I've been testing on a UnityRemote, however I'm trying to restart my main scene by touching with more than one finger on an android. endCondition is a variable I set as active when the player has been destroyed. Here is what my code looks like. All I want to do is restart the scene with multi-touch. Neither of the prints logged to the console so I doubt the multiple touches are being recognized. Here is my code that is inside of Update()
if (endCondition && (Input.touchCount > 1))
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch t = Input.GetTouch(i);
if (t.phase == TouchPhase.Began)
{
print("Made it here");
}
if (t.phase == TouchPhase.Ended)
{
print("Made it here");
SceneManager.LoadScene("Main");
}
}
}
This should fix it:
if (endCondition)
{
int fingersAmount = Input.touchCount;
//1 FINGER
if (fingersAmount == 1)
{
Touch t = Input.GetTouch(fingersAmount - 1);
if (t.phase == TouchPhase.Began)
{
Debug.Log("ONE FINGER");
}
if (t.phase == TouchPhase.Ended)
{
Debug.Log("ONE FINGER END");
//SceneManager.LoadScene("Main");
}
}
//2 FINGERS
else if (fingersAmount == 2)
{
Touch t = Input.GetTouch(fingersAmount - 1);
if (t.phase == TouchPhase.Began)
{
Debug.Log("TWO FINGERS");
}
if (t.phase == TouchPhase.Ended)
{
Debug.Log("TWO FINGERS END");
//SceneManager.LoadScene("Main");
}
}
//3 FINGERS
else if (fingersAmount == 3)
{
Touch t = Input.GetTouch(fingersAmount - 1);
if (t.phase == TouchPhase.Began)
{
Debug.Log("THREE FINGERS");
}
if (t.phase == TouchPhase.Ended)
{
Debug.Log("THREE FINGERS END");
//SceneManager.LoadScene("Main");
}
}
}

Swift: prevent tic tac toe double tap?

I have a tic tac toe game here in Swift and I need a way to disable tap gesture recognition on spaces that have been played so that on their turn, the user cannot just tap places that have been played.
I have tried putting the line
ticTacImages[spot].removeGestureRecognizer(UITapGestureRecognizer(target: self, action: "imageClicked:"))
in my imageClicked and setImageForSpot functions and nothing happens. What am I doing wrong here?
The code involved:
for imageView in ticTacImages {
imageView.userInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "imageClicked:"))
}
}
//Gesture Reocgnizer method
func imageClicked(reco: UITapGestureRecognizer) {
var imageViewTapped = reco.view as UIImageView
println(plays[imageViewTapped.tag])
println(aiDeciding)
println(done)
opening1.hidden = true
opening2.hidden = true
opening3.hidden = true
if plays[imageViewTapped.tag] == nil && !aiDeciding && !done {
setImageForSpot(imageViewTapped.tag, player:.UserPlayer)
}
checkForWin()
let delay = 1 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
//During delay
for imageView in ticTacImages {
imageView.userInteractionEnabled = false
}
dispatch_after(time, dispatch_get_main_queue(), {
//After delay
for imageView in self.ticTacImages {
imageView.userInteractionEnabled = true
}
self.aiTurn()
})
}
var varChanger: Int?
var playerMark: String?
func setImageForSpot(spot:Int,player:Player){
if varChanger == 1 {
playerMark = player == .UserPlayer ? "blue_x" : "blue_o"
}
else if varChanger == 2 {
playerMark = player == .UserPlayer ? "green_x" : "green_o"
}
else if varChanger == 3 {
playerMark = player == .UserPlayer ? "purple_x" : "purple_o"
}
else if varChanger == 4 {
playerMark = player == .UserPlayer ? "pink_x" : "pink_o"
}
else if varChanger == 5 {
playerMark = player == .UserPlayer ? "yellow_x" : "yellow_o"
}
else {
playerMark = player == .UserPlayer ? "red_x" : "red_o"
}
println("setting spot \(player.toRaw()) spot \(spot)")
plays[spot] = player.toRaw()
ticTacImages[spot].image = UIImage(named: playerMark)
}
You can only removeGestureRecognizer() for a gesture recognizer that has already been added. In your example you're creating a new one before removing it — instead you should keep track of the old one, or call ticTacImages[spot].gestureRecognizers to get an array of the ones which have been added.

Need help on rotating image view with animation on orientation change (4 side rotation) in android

i have a image view and i need to rotate(with IOS like animation) the image view on orientation change in android(portrait,landscape,reverse portrait and reverse landscape).
please advice
void rotateAndSet(int angle) {
if (currentAngle != angle || currentImage != currentBaseImage) { // This
// is
// to
// remove
// unnecessary
// drawing
currentAngle = angle;
currentImage = currentBaseImage;
myImg = decodeBase64(currentBaseImage);
matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
myImg.getHeight(), matrix, true);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
param.addRule(RelativeLayout.ALIGN_PARENT_TOP);
/*RotateAnimation animation = new RotateAnimation(0, 90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setFillAfter(true);
animation.setDuration(800);
ivFullScreen.startAnimation(animation); */
ivFullScreen.setLayoutParams(param);
ivFullScreen.setImageBitmap(rotated);
}
}
i got it
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
RotateAnimation animation = null;
if (event.values[0] < 4 && event.values[0] > -4) {
if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) {
// UP
orientation = ExifInterface.ORIENTATION_ROTATE_90;
Log.i("testme","up");
animation = getRotateAnimation(0);
degrees = 0;
} else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) {
// UP SIDE DOWN
Log.i("testme","up side down");
orientation = ExifInterface.ORIENTATION_ROTATE_270;
animation = getRotateAnimation(180);
degrees = 180;
}
} else if (event.values[1] < 4 && event.values[1] > -4) {
if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) {
// LEFT
Log.i("testme","left");
orientation = ExifInterface.ORIENTATION_NORMAL;
animation = getRotateAnimation(-270);
degrees =-270;
} else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) {
// RIGHT
Log.i("testme","right");
orientation = ExifInterface.ORIENTATION_ROTATE_180;
animation = getRotateAnimation(270);
degrees = 270;
}
}
if (animation != null) {
ivFullScreen.startAnimation(animation);
}
}
}
}

How to determine the direction of a iPhone shake

I am using the accelerometer to scroll multiple subViews in a UIScrollVIew. I want the view (portrait orientation) to scroll to the right when the user flicks the iPhone to the right, and scroll to the left when the device is flicked to the left.
I thought I could do that just by noting positive or negative x acceleration values, but I see that the values are usually a mixture of positive and negative values. I have set the floor at 1.5g to eliminate non-shake movement, and am looking at the x values over the duration of .5 seconds.
I'm sure there is a trigonometrical method for determining the overall direction of a flick, and that you have to measure values over the duration of the flick motion. I'm also sure that someone has already figured this one out.
Any ideas out there?
Thanks
I developed a solution which gives me better feedback than the proposed solution (only for left and right shake).
The way I did it here is quite sensitive (recognizes a small shake), but sensitivity can be adapted by changing tresholdFirstMove and tresholdBackMove (increase for lower sensitivity)
In Swift :
(in your viewController. And add "import CoreMotion")
var startedLeftTilt = false
var startedRightTilt = false
var dateLastShake = NSDate(timeIntervalSinceNow: -2)
var dateStartedTilt = NSDate(timeIntervalSinceNow: -2)
var motionManager = CMMotionManager()
let tresholdFirstMove = 3.0
let tresholdBackMove = 0.5
override func viewDidLoad() {
// your code
motionManager.gyroUpdateInterval = 0.01
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
motionManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: { (gyroData, error) -> Void in
self.handleGyroData(gyroData.rotationRate)
})
}
private func handleGyroData(rotation: CMRotationRate) {
if fabs(rotation.z) > tresholdFirstMove && fabs(dateLastShake.timeIntervalSinceNow) > 0.3
{
if !startedRightTilt && !startedLeftTilt
{
dateStartedTilt = NSDate()
if (rotation.z > 0)
{
startedLeftTilt = true
startedRightTilt = false
}
else
{
startedRightTilt = true
startedLeftTilt = false
}
}
}
if fabs(dateStartedTilt.timeIntervalSinceNow) >= 0.3
{
startedRightTilt = false
startedLeftTilt = false
}
else
{
if (fabs(rotation.z) > tresholdBackMove)
{
if startedLeftTilt && rotation.z < 0
{
dateLastShake = NSDate()
startedRightTilt = false
startedLeftTilt = false
println("\\\n Shaked left\n/")
}
else if startedRightTilt && rotation.z > 0
{
dateLastShake = NSDate()
startedRightTilt = false
startedLeftTilt = false
println("\\\n Shaked right\n/")
}
}
}
}
OK, worked out a solution. When I detect a shake motion (acceleration greater than 1.5 on the x axis), I start a timer and set a BOOL to true. While the BOOL is true I add acceleration values. When the timer expires, I stop adding acceleration values and determine direction of the shake by the sign of the total acceleration.
- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler {
if (fabsf(aceler.x) > 1.5)
{
shake = YES;
NSTimeInterval myInterval = .75;
[NSTimer scheduledTimerWithTimeInterval:myInterval target:self selector:#selector(endShake) userInfo:nil repeats:NO];
return;
}
if(shake)
{
totalG += aceler.x;
}
}
- (void) endShake {
shake = NO;
int direction;
if (totalG isLessThan 0) direction = 1;
if(totalG isGreaterThan 0) direction = -1;
[self changePageByShake:direction];
totalG = 0;
}
Note: I couldn't get the < and > symbols to format correctly in the codeblock above, so I substituted isLessThan and isGreaterThan for the symbols.