UIImageView - How to change speed of reverse animation - iphone

I have the following code which animates a volume meter:
float meterIndexFloat = volume * 6;
int meterIndex = (int)roundf(meterIndexFloat);
NSMutableArray *litImages = [[NSMutableArray alloc] init];
for(int i=0; i < meterIndex; i++) {
[litImages addObject:[volumeMeterImages objectAtIndex:i]];
}
volumeMeter.animationImages = litImages;
volumeMeter.animationDuration = 0.4;
volumeMeter.animationRepeatCount = 1;
[volumeMeter startAnimating];
It works well, but I would like to slow down the animation when it reverses itself. Right now the duration is 0.4, but I would like to have it slow down to say 0.7 when the images go in the opposite direction. What's the easiest way to modify it to do this?
Thanks!

Related

How to use the same Cocos2d image (single .png) in SpriteKit Texture Atlas (to multiple .png)

I have a SINGLE .png (generated from texturepacker) that i used in cocos2d to animate a button. Now, I am migrating to SpriteKit (have to) and in using texture atlas it has to be MULTIPLE split files. I tried to split it thru Shoebox but I can't get the right sequence. How do i do thesame animation?
If I understand the question correctly you are most likely looking for something like this.
SKTexture *fullTexture = [SKTexture textureWithImageNamed:#"single.png"];
float widthPercent = 1.0f/(float)columns;
float heightPercent = 1.0f/(float)rows;
NSMutableArray *textureArray = [[NSMutableArray alloc]init];
for (NSInteger i = 0; i < rows; i++)
{
for (NSInteger j = 0; j < columns; j++)
{
SKTexture *texture = [SKTexture textureWithRect:CGRectMake(j*widthPercent, i*heightPercent, widthPercent, heightPercent) inTexture:fullTexture];
[textureArray addObject:texture];
}
}
SKAction *animate = [SKAction animateWithTextures:textureArray timePerFrame:.2];
SKAction *repeatAction = [SKAction repeatActionForever:animate];
[yourSprite runAction:repeatAction];
You will want to look at the docs for textureWithRect:inTexture: here for more information. Remember it is a range of 0 to 1.
I hope that answers your question or at least gets you started in the right direction.

UITabBar changing each time to a new tab, without scrolling to middle

I want to create an tabBar with 15 items that can be scroll right-left and stop in the middle and not just scrolling each time all the 5 items (320 every time)
I found a code and changed it for displaying 5 items, but when i'm scroll it, all the tab changed and the next tab with 5 items shown, and so on...
- (id)initWithItems:(NSArray *)items {
self = [super initWithFrame:CGRectMake(0.0, 411.0, 320.0, 49.0)];
if (self) {
self.pagingEnabled = YES;
self.delegate = self;
self.tabBars = [[[NSMutableArray alloc] init] autorelease];
float x = 0.0;
for (double d = 0; d < ceil(items.count / 5.0); d ++) {
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(x, 0.0, 320.0, 49.0)];
tabBar.delegate = self;
int len = 0;
for (int i = d * 5; i < d * 5 + 5; i ++)
if (i < items.count)
len ++;
tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * 5, len)]];
[self addSubview:tabBar];
[self.tabBars addObject:tabBar];
[tabBar release];
x += 320.0;
}
self.contentSize = CGSizeMake(x, 49.0);
}
return self;
}
How can i create a 'rubber effect' so i can be stop on the 7 item for example.
You will have to use a different design than the one you quote. If you look at the code carefully you will notice that for each 5 items a new tab bar is created. Clearly, the functionality that you hope for is not feasible with this setup.
The alternative would be to rewrite this with your own UIScrollView. It could be challenging to get exactly the look and feel of the UITabBar, but perhaps that is not such an important constraint. Rather, you would be free to implement your very own design that harmonizes with the look of your app.

Move images to random placeholders

I have a simple jigsaw puzzle i'm making. I have a method that is called when the view is loaded and when the device is shaken. This method places 4 images in 4 specific places on screen. Below is the code:
-(void) makePieceHolders {
//create 4 points where the jigsaw pieces (images) will be placed
CGPoint holder1 = CGPointMake(80, 80);
CGPoint holder2 = CGPointMake(200, 80);
CGPoint holder3 = CGPointMake(80, 200);
CGPoint holder4 = CGPointMake(200, 200);
image1.center = holder1; //set the position of the image center to one of the newly created points
image1.alpha = 0.3; //set the image opacity back to 0.3
image2.center = holder2;
image2.alpha = 0.3;
image3.center = holder3;
image3.alpha = 0.3;
image4.center = holder4;
image4.alpha = 0.3;
}
What i'd like is to place the images randomly in the four placeholders. I have some more code written below where i get a random number between 1 and 4 and set the tag of each image to each of these random numbers.
int randomNumber;
int placeHolders[4];
int i=0;
bool numberFound;
do{ // until you get 4 unique numbers
randomNumber=arc4random()%4+1;
// Does this number exist already?
numberFound=FALSE;
for (int j=0; j<i; j++) {
if (placeHolders[j]==randomNumber)
numberFound=TRUE;
}
if (numberFound==FALSE){
placeHolders[i]=randomNumber;
i++;
}
} while (i<4);
image1.tag = placeHolders[0];
image2.tag = placeHolders[1];
image3.tag = placeHolders[2];
image4.tag = placeHolders[3];
NSLog(#"img1 tag: %i img2 tag: %i img3 tag: %i img4 tag: %i", image1.tag, image2.tag, image3.tag, image4.tag);
How do now refer to this tag information in order to move it to a placeholder?
In pseudocode i was thinking:
where image tag = 1, move that image to holder1
where image tag = 2, move that image to holder2
............
I don't know how to write this though.
If there is a better way i'd appreciate the help. Thanks
You don't need your complicated do..while / tag logic.
Just use an array:
NSMutableArray* images = [NSMutableArray arrayWithObjects: image1,image2,image3,image4,nil];
// shuffle the array
NSUInteger count = [images count];
for (NSUInteger i = 0; i < count; i++) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[images exchangeObjectAtIndex:i withObjectAtIndex:n];
}
After that, you have randomly placed your images in a new order. After that assign the positions:
UIImageView* imageView1 = (UIImageView*)[images objectAtIndex: 0];
imageView.center = holder1;
UIImageView* imageView2 = (UIImageView*)[images objectAtIndex: 1];
imageView.center = holder2;
UIImageView* imageView3 = (UIImageView*)[images objectAtIndex: 2];
imageView.center = holder3;
UIImageView* imageView4 = (UIImageView*)[images objectAtIndex: 3];
imageView.center = holder4;
(You could also do this in a loop.. so it would be more general and reusable.)

Please help! Creating new objects (from array?) after an object completes task

I am writing an iPhone application and here is the overall synopsis:
An object is on the screen and moves based on accelerometer input - I have that working - it moves to the edge of the screen and doesn't go off = perfect. Now, what I want to happen is as soon as that object hits any of the four screen edges, it should stop and stay put on the screen, and a new object should 'appear' and start moving due to the accelerometer input, so now two objects would be on the screen, but only 1 moving. Eventually there could be 20 objects built up around the edge, but only 1 object will be moving at a time.
So I have now gotten the help I needed to check for edge hits etc, but I am now trying to switch the way I was getting boxes to show up on the screen. I originally was putting images on the screen through the view controller, but now what I want to do is start with one box in the center, when it hits an edge, it should stop and stay, and a new image will appear in the center and start moving due to accel input as described above. So do I just use an array to pull the images from? Do I not even put .png's on the view controller and should I just code it? Here is some of what I have trying to do this through an array:
//In my .h
UIImageView *blocks;
NSString *blockTypes[3];
//In my .m
blockTypes[0] = #"greenBox1.png";
blockTypes[1] = #"greenBox2.png";
blockTypes[2] = #"greenBox3.png";
Thanks in advance for any help! The help so far has been great!
You should't test if newX and newY are equal to 30 and 50. You should test if they are less than 30 and 50 respectively.
Edit:
I would do it like this:
loat newX = [mutableBoxArray lastObject].center.x + (accel.x * 12);
float newY = [mutableBoxArray lastObject].center.y + (accel.y * -12);
if(newX > 30 && newY > 50 && newX < 290 && newY < 430) {
[[mutableBoxArray lastObject] setCenter: CGPointMake(newX, newY)];
} else {
MyBox *myBox = [[MyBox alloc] init];
[mutableBoxArray addObject: myBox];
[myBox release];
}
Edit 2:
Add the following in your interface file
NSMutableArray *mutableBoxArray;
NSArray *imageNamesArray;
Then in your implementation file in the loadView add
mutableBoxArray = [[NSMutableArray alloc] init];
imageNamesArray = [[NSArray alloc] initWithObjects: #"orangeBox1.png",
#"blueBox1.png", #"greenBox1.png", #"pinkBox1.png", nil];
Then change the above method to
static NSInteger imageInt = 0;
loat newX = [mutableBoxArray lastObject].center.x + (accel.x * 12);
float newY = [mutableBoxArray lastObject].center.y + (accel.y * -12);
if(newX > 30 && newY > 50 && newX < 290 && newY < 430) {
[[mutableBoxArray lastObject] setCenter: CGPointMake(newX, newY)];
} else {
if (imageInt < [imageNamesArray count]) {
UIImage *image = [UIImage imageNamed: [imageNamesArray objectAtIndex: imageInt++]];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setCenter: CGPointMake(100.0f, 100.0f)];
[mutableBoxArray addObject: imageView];
[imageView release];
}
}

What is the best programmatically approach to display images/icons horizontally?

I'm trying to place the icons / images horizontally in a view that is intended to show details. Maximum number of pictures per row is 6 pieces and I want to have a mechanism that manage line breaks or similar automatically. I suspect it is a custom cell that solves this?
I have tried the following code below, but the images above to add another when I reload the view. Furthermore, the cell's height is not adjusted by the view that the code returned.
-(UIImageView *)fillView{
collage = nil;
collage = [[[UIImageView alloc] initWithFrame:CGRectMake(11, 7, 0, 0)] autorelease];
int rowCounter = 0;
int colCounter = 0;
int nrOfPictures = [paymentImages count];
//max 6 images per row
while (nrOfPictures > 0) {
while (colCounter <= 6 && nrOfPictures != 0) {
UIImageView *iv = [[UIImageView alloc] initWithImage:[paymentImages objectAtIndex:nrOfPictures-1]];
CGRect frame = iv.frame;
frame.origin.x = (frame.size.width + 4) * colCounter;
frame.origin.y = (frame.size.height + 4) * rowCounter;
iv.frame = frame;
[collage addSubview:iv];
[iv release];
colCounter++;
nrOfPictures--;
if (colCounter > 6) {
colCounter = 0;
rowCounter++;
}
}
}
return collage;
}
Can someone head me in the right direction?
You might want to check out this project. http://github.com/kirbyt/KTPhotoBrowser