Fixed numbers in a switch statement (Random) - ios

I'm new to Xcode and i'm really stuck.
I have a UIProgressView which fills up with a UISwipeGestureRecognizer. I also have a switch statement. Now I want that only 7 ways to run (from the 242) while progressTrue is false. If progressTrue is true, so the UIProgressView is on 1.0, it should select new 7 cases randomly. Is something like that possible?
Thanks for any help!
-(void)vorgangDev {
int number = arc4random() % 242;
switch (number) {
case 0:
question.text = #"Anlieferung/anliefern";
answer.text = #"delivery, supply/deliver, supply";
answer.hidden = YES;
break;
case 1:
question.text = #"Artikel";
answer.text = #"article, item";
answer.hidden = YES;
break;
case 2:
question.text = #"Artikelanzahl";
answer.text = #"number of articles/ ~items";
answer.hidden = YES;
break;
//And so on...
}
Here I call vorgangDev.
if (progressTrue.progress == 1.0) {
// The UIProgressView is full
// if this is true do the question.
CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:#"backgroundColor"];
fade.fromValue = (id)[UIColor blackColor].CGColor;
fade.toValue = (id)[UIColor colorWithRed:50/255.0f green:109/255.0f blue:43/255.0f alpha:1.0f].CGColor;
[fade setDuration:1];
[self.view.layer addAnimation:fade forKey:#"fadeAnimation"];
progressTrue.progress = 0.0;
[self vorgangDev];

Related

Why UILongPressGestureRecognizer behaves differently on devices released after iPhone 6?

In my code, I have an area on which when users taps and holds, UILongPressGestureRecognizer causes an animation to run and at the end of animation a method gets called.
(It's basically a long tap on a small area square which contains one CGPoint so that it starts a long press animation and when it finishes, the point is removed.)
The problem: On iPhone 5s,SE and 6, UILongPressGestureRecognizer works fine but on any device released after that, it gets dismissed very quickly and I have tried it on at least 11 different devices.
Why is that?!
I appreciate any help.
-(void)prepareLongPressGesture{
_longGesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongGesture:)];
_longGesture.delegate = self;
_longGesture.allowableMovement = self.frame.size.width*0.15;
_longGesture.minimumPressDuration = 1.0;
[self addGestureRecognizer:_longGesture];
}
- (void)handleLongGesture:(UILongPressGestureRecognizer*)gesture {
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
[self removeModifiablePoint];
break;
case UIGestureRecognizerStateChanged:
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled: {
if (self.animatedView.superview) {
[self.animatedView removeFromSuperview];
}
} break;
default:
break;
}
}
- (void)removeModifiablePoint {
CGFloat sizeOFLongPressAnimation = 0.0;
if (IS_IPAD) {
sizeOFLongPressAnimation = 0.15*self.frame.size.width;
}else{
sizeOFLongPressAnimation = 0.27*self.frame.size.width;
}
if (!_animatedView) {
self.animatedView =
[[UIView alloc] initWithFrame:CGRectMake(0, 0, sizeOFLongPressAnimation, sizeOFLongPressAnimation)];
self.animatedView.alpha = 0.7f;
self.animatedView.layer.borderColor = [UIColor redColor].CGColor;
self.animatedView.layer.borderWidth = 3.f;
self.animatedView.layer.cornerRadius = sizeOFLongPressAnimation / 2.0f;
self.progressView = [[UIView alloc] initWithFrame:self.animatedView.bounds];
self.progressView.backgroundColor = [UIColor redColor];
self.progressView.layer.cornerRadius = sizeOFLongPressAnimation / 2.0f;
[self.animatedView addSubview:self.progressView];
}
CGPoint center = [self.modifiablePointsArray[_modifiablePointIndex] CGPointValue];
self.animatedView.center = center;
[self addSubview:self.animatedView];
self.progressView.transform =
CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1f);
[UIView animateWithDuration:2.f animations:^{
self.progressView.transform = CGAffineTransformIdentity;
}completion:^(BOOL finished) {
if (finished) {
[self.animatedView removeFromSuperview];
[self.modifiablePointsArray removeObjectAtIndex:_modifiablePointIndex];
[self setNeedsDisplay];
}
}];
}

How to Animate a Path Forwards and Backwards?

I have animated a UIBezier Path to go around a circle and change its stroke color whilst doing so. When it goes all the way around, I would like it go all the way around in the opposite direction.
Here is the code that makes it go forwards:
[CATransaction begin];
{
[CATransaction setAnimationDuration: 3.0];//Dynamic Duration
[CATransaction setCompletionBlock:^{
[tutorialCircle removeFromSuperlayer];
tutorialCircle.opacity = 0.0;
strokePart.opacity = 0.0;
[strokePart removeFromSuperlayer];
}];
const double portion = 1.0 / ((double)3);
for (NSUInteger i = 0; i < 3; i++)
{
strokePart = [[CAShapeLayer alloc] init];
strokePart.fillColor = [[UIColor clearColor] CGColor];
strokePart.frame = tutorialCircle.bounds;
strokePart.path = tutorialCircle.path;
strokePart.lineCap = tutorialCircle.lineCap;
strokePart.lineWidth = tutorialCircle.lineWidth;
if (i == 0) {
strokePart.strokeColor = [UIColor greenColor].CGColor;
}
else if (i == 1) {
strokePart.strokeColor = [UIColor orangeColor].CGColor;
}
else if (i == 2) {
strokePart.strokeColor = [UIColor redColor].CGColor;
}
strokePart.strokeStart = i * portion;
strokePart.strokeEnd = (i + 1) * portion;
[tutorialCircle addSublayer: strokePart];
animation = [CAKeyframeAnimation animationWithKeyPath: #"strokeEnd"];
NSArray* times = #[ #(0.0), // Note: This works because both the times and the stroke start/end are on scales of 0..1
#(strokePart.strokeStart),
#(strokePart.strokeEnd),
#(1.0) ];
NSArray* values = #[ #(strokePart.strokeStart),
#(strokePart.strokeStart),
#(strokePart.strokeEnd),
#(strokePart.strokeEnd) ];
animation.keyTimes = times;
animation.values = values;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
[animation setDelegate:self];
[strokePart addAnimation: animation forKey: #"whatever"];
}
}
[CATransaction commit];
So in my animationDidStop delegate method for the first animation, I know I'd be calling the second animation to go backwards, however, I am struggling to create the animation for this to go backwards.
Have you tried?
animation.autoreverses = YES

Sprite Kit - Add random Sprite Nodes to the scene with Switch Case

I have the following code to create random objects and add them to the scene. At the end the method is repeated after a random delay.
-(void)createObjects {
// Create random start point
float randomStartPoint = arc4random_uniform(4) * 64 + 32;
CGPoint startPoint = CGPointMake(self.size.width + 50, randomStartPoint);
// Create random object and add to scene
switch (arc4random_uniform(2)) {
case 0:
{
SKSpriteNode *crate = [SKSpriteNode spriteNodeWithImageNamed: #"crate"];
crate.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:crate.frame.size];
crate.physicsBody.dynamic = YES;
crate.physicsBody.affectedByGravity = NO;
crate.physicsBody.categoryBitMask = objectCategory;
crate.physicsBody.collisionBitMask = 0;
crate.physicsBody.contactTestBitMask = playerCategory;
crate.position = startPoint;
crate.name = #"object";
crate.zPosition = 20;
[self addChild:crate];
break;
}
case 1:
{
SKSpriteNode *cactus = [SKSpriteNode spriteNodeWithImageNamed: #"cactus"];
cactus.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cactus.frame.size];
cactus.physicsBody.dynamic = YES;
cactus.physicsBody.affectedByGravity = NO;
cactus.physicsBody.categoryBitMask = objectCategory;
cactus.physicsBody.collisionBitMask = 0;
cactus.physicsBody.contactTestBitMask = playerCategory;
cactus.position = startPoint;
cactus.name = #"object";
cactus.zPosition = 20;
[self addChild:cactus];
break;
}
default:
break;
}
// After adding child, call same method at random delay
float randomNum = arc4random_uniform(4) * 0.4 + 0.25;
[self performSelector:#selector(createObjects) withObject:nil afterDelay:randomNum];
}
In reality I have many more cases and a lot of the same code is repeated, since every Sprite Node has the same settings. Is there a way to create a sprite node once, and in every case set a different image for the node, and add it to the scene?
You can create SKTexture in switch case and then simply create node with that texture.
Like this:
-(void)createObjects {
// Create random start point
float randomStartPoint = arc4random_uniform(4) * 64 + 32;
CGPoint startPoint = CGPointMake(self.size.width + 50, randomStartPoint);
// Create random object and add to scene
SKTexture* objectTexture;
switch (arc4random_uniform(2)) {
case 0:
objectTexture = [SKTexture textureWithImageNamed:#"crate"];
break;
case 1:
objectTexture = [SKTexture textureWithImageNamed:#"cactus"];
break;
default:
break;
}
SKSpriteNode *object = [SKSpriteNode spriteNodeWithTexture:objectTexture];
object.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:crate.frame.size];
object.physicsBody.dynamic = YES;
object.physicsBody.affectedByGravity = NO;
object.physicsBody.categoryBitMask = objectCategory;
object.physicsBody.collisionBitMask = 0;
object.physicsBody.contactTestBitMask = playerCategory;
object.position = startPoint;
object.name = #"object";
object.zPosition = 20;
[self addChild: object];
}
Another way would be to create object before switch case and then simply create texture and set it in switch case using setTexture:

multiple shadowOffset on UIViewController

I've looked on the web but my results mainly consist of "how to do a shadowOffset." I need to apply "Shadow,Border, and CornerRadius" to multiple objects in my views. I just wanted to see if theres a more effienct way? Or if theres a way to keep it more organized. heres some of my code. Keep in mind I have multiple views like this, so the amount of space this code takes gets pretty annoying.
topView.layer.cornerRadius = 3;
topView.layer.masksToBounds = YES;
topView.layer.borderColor= [UIColor lightGrayColor].CGColor;
topView.layer.borderWidth = 0.5f;
bottomView.layer.cornerRadius = 3;
bottomView.layer.masksToBounds = YES;
bottomView.layer.borderColor= [UIColor lightGrayColor].CGColor;
bottomView.layer.borderWidth = 0.5f;
eventName.layer.masksToBounds = NO;
eventName.layer.shadowColor = [UIColor blackColor].CGColor;
eventName.layer.shadowOpacity = 0.5;
eventName.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventName.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
addressLabel.layer.masksToBounds = NO;
addressLabel.layer.shadowColor = [UIColor blackColor].CGColor;
addressLabel.layer.shadowOpacity = 0.5;
addressLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
addressLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
dateLabel.layer.masksToBounds = NO;
dateLabel.layer.shadowColor = [UIColor blackColor].CGColor;
dateLabel.layer.shadowOpacity = 0.5;
dateLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
dateLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
typeLabel.layer.masksToBounds = NO;
typeLabel.layer.shadowColor = [UIColor blackColor].CGColor;
typeLabel.layer.shadowOpacity = 0.5;
typeLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
typeLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
eventCaption.layer.masksToBounds = NO;
eventCaption.layer.shadowColor = [UIColor blackColor].CGColor;
eventCaption.layer.shadowOpacity = 0.5;
eventCaption.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventCaption.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
If most of your shadows are identical, you could choose to loop over an array (or a set, since you don't really care about the order of the items it is applied to) of your views to apply the same shadow ?
That is, I see 2 kind of shadows in the code you pasted here, the lightGray and the black.
You could do something like :
NSArray * blackShadowItems = #[eventName, addressLabel, dateLabel];
for (UIView * view in blackShadowItems) {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
or declare another function :
- (void)setBlackShadow:(UIView *)view {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
You can combine both of these solutions.
Finally, note that if these views are declared in a .xib file, you could declare an IBOutletCollection to regroup the views according to the type of shadow you want to set on it.
This is some quite similar to declaring your NSArray or NSSet yourself.

iOS CAEmitterLayer how to show animation on uiimageview

I am following this tutorial "http://www.raywenderlich.com/6063/uikit-particle-systems-in-ios-5-tutorial". everything works but I want that effect to go on my image view.
please help where should I make changes
this is code I have in my view controller on which I am showing animation
-(IBAction)ClickOn:(id)sender
{
[fireView startAnimation];
[fireView setIsEmitting:YES];
[self.view bringSubviewToFront:fireView];
[self.view sendSubviewToBack:sample];
}
and this is code in library view class
-(void)startAnimation
{
NSLog(#"awakeFromNib");
//set ref to the layer
fireEmitter = (CAEmitterLayer*)self.layer; //2
//configure the emitter layer
fireEmitter.emitterSize = CGSizeMake(20, 20);
CAEmitterCell* fire = [CAEmitterCell emitterCell];
fire.birthRate = 0;
fire.lifetime = 2.0;
fire.lifetimeRange = 1.5;
fire.color = [[UIColor colorWithRed:236 green:237 blue:237 alpha:0.1] CGColor];
fire.contents = (id)[[UIImage imageNamed:#"Particles_fire.png"] CGImage];
[fire setName:#"fire"];
fire.yAcceleration =
fire.velocity = 0.1;
fire.velocityRange = 80;
fire.emissionRange = 80;
fire.scale = 1.0;
fire.scaleSpeed = 0.1;
fire.spin = 40.5;
CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:#"emitterPosition"];
ba.fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 400)];
ba.toValue = [NSValue valueWithCGPoint:CGPointMake(300,0)];
ba.duration = 6;
ba.autoreverses = NO;
[fireEmitter addAnimation:ba forKey:nil];
fireEmitter.renderMode = kCAEmitterLayerPoints;
//add the cell to the layer and we're done
fireEmitter.emitterCells = [NSArray arrayWithObject:fire];
fireEmitter.zPosition = 400.0;
}
basically i just want animation to go above uiimageview or uiview, currently it is going behind it

Resources