I have image view, if click button talking tom cat will play with animationRepeatCount:2, after this, I want to hide image view. How to hide that image view?
- (IBAction)catBlinkButton:(UIButton *)sender {
self.imageView.hidden = NO;
self.imageView.animationDuration = 2;
self.imageView.animationRepeatCount = 2;
self.imageView.animationImages = [[NSArray alloc]initWithObjects:
[UIImage imageNamed:#"cat_blink0000.png"],
[UIImage imageNamed:#"cat_blink0001.png"],
[UIImage imageNamed:#"cat_blink0002.png"],
nil];
[self.imageView startAnimating];
}
This is my code...
You can simply use CATransaction like:
self.imageView.hidden = NO;
[CATransaction begin];
[CATransaction setCompletionBlock:^{
self.imageView.hidden = YES;
}];
self.imageView.animationDuration = 2;
self.imageView.animationRepeatCount = 2;
self.imageView.animationImages = [[NSArray alloc]initWithObjects:
[UIImage imageNamed:#"cat_blink0000.png"],
[UIImage imageNamed:#"cat_blink0001.png"],
[UIImage imageNamed:#"cat_blink0002.png"],
nil];
[self.imageView startAnimating];
[CATransaction commit];
CATransaction will dynamically wait for the animation to finish...
You can execute the code after slate on main thread.
- (IBAction)catBlinkButton:(UIButton *)sender {
self.imageView.hidden = NO;
self.imageView.animationDuration = 2;
self.imageView.animationRepeatCount = 2;
self.imageView.animationImages = [[NSArray alloc]initWithObjects:
[UIImage imageNamed:#"cat_blink0000.png"],
[UIImage imageNamed:#"cat_blink0001.png"],
[UIImage imageNamed:#"cat_blink0002.png"],
nil];
[self.imageView startAnimating];
// after 2 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// hide imageview
self.imageView.hidden = YES;
});
}
You can do it with UIView's hidden property.
imageView.hidden = true;
- (IBAction)catBlinkButton:(UIButton *)sender {
self.imageView.hidden = NO;
self.imageView.animationDuration = 2;
self.imageView.animationRepeatCount = 2;
self.imageView.animationImages = [[NSArray alloc]initWithObjects:
[UIImage imageNamed:#"cat_blink0000.png"],
[UIImage imageNamed:#"cat_blink0001.png"],
[UIImage imageNamed:#"cat_blink0002.png"],
nil];
[self.imageView startAnimating];
}
Related
I have an app which displays some basic animation triggered by the playback of midi file. It works ok during the first playback but freezes during subsequent playbacks. The device then usually reboots generating a crash log indicating that a kernel panic took place. Occasionally it just hangs without a reboot.
The animation consists of an small png image which has the opacity changed from high to low to give a fading effect.
The freeze takes place on the line :[self performSelectorOnMainThread:#selector( setPitch: ) withObject:[NSNumber numberWithInteger:[note intValue]] waitUntilDone:YES];
Here is the method which contains this line:
- (void)log:(NSNotification *)notification {
[NSThread isMainThread];
NSDictionary* info = notification.userInfo;
NSNumber *note;
note = [info objectForKey:kNAMIDI_Note];
BRMidiNoteName *noteConverter = [[BRMidiNoteName alloc] init];
NSString *noteName;
noteName = [noteConverter nameFromNumber:[note intValue] withNotation:[defaults valueForKey:kSettingsNotation]];
[self.currentNoteLabel performSelectorOnMainThread: #selector( setText: ) withObject: noteName waitUntilDone: YES];
[self performSelectorOnMainThread:#selector( setPitch: ) withObject:[NSNumber numberWithInteger:[note intValue]] waitUntilDone:YES];
}
-(void) setPitch:(NSNumber*) pitchValue{
NSInteger visualPitchValue = [pitchValue intValue]- [_currentTrack.trackStartNote intValue] - voiceTransposeValue + 1;
if ([defaults boolForKey:kSettingsVisualiser]) {
//UIImageView *img = [[UIImageView alloc]init];
UIImageView *img;
if ((visualPitchValue > 0 )) {
// highlight new pitch
img = (UIImageView *)[self.view viewWithTag:visualPitchValue];
if (([pitchValue integerValue] < [currentVoice.bridgeLow integerValue])) {
[img setImage:[UIImage imageNamed:#"bar_green.png"]];
}
else if (([pitchValue integerValue] <= [currentVoice.bridgeHigh integerValue])) {
[img setImage:[UIImage imageNamed:#"bar_orange"]];
}
else{
[img setImage:[UIImage imageNamed:#"bar_blue.png"]];
}
[img setAlpha:1.0];
// Set up fade out effect
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
[fadeOutAnimation setToValue:[NSNumber numberWithFloat:_fadeToOpacity]];
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.removedOnCompletion = NO;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
// [group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, nil]];
[group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, nil]];
group.duration = 0.7f;
group.delegate = self;
[group setValue:img forKey:#"imageViewBeingAnimated"];
[img.layer addAnimation:group forKey:#"savingAnimation"];
}
}
}
I found that changing the waitUntilDone from YES to NO stopped the device screen from freezing.
[self.currentNoteLabel performSelectorOnMainThread: #selector( setText: ) withObject: noteName waitUntilDone: NO];
[self performSelectorOnMainThread:#selector( setPitch: ) withObject:[NSNumber numberWithInteger:[note intValue]] waitUntilDone:NO];
I have the following code to play animation with images:
-(void)playPopAnimation:(void (^)(void))completion{
__block NSMutableArray *images;
float animationDuration = 1.0f;
images = [[NSMutableArray alloc] initWithCapacity:26];
for(int i=1; i<26; i++){
NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"bulut_pop%d",i] ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];
//UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"bulut_pop%d.png",i]];
[images addObject:image];
}
[Helper playSound:#"button_pop"];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.itemImage.frame];
imageView.animationImages = images;
imageView.animationDuration = animationDuration;
imageView.animationRepeatCount = 1; // 0 = nonStop repeat
[self addSubview:imageView];
[imageView startAnimating];
self.imageButton.userInteractionEnabled = NO;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, animationDuration * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
//[self.delegate myItemCell:self cellDidTappedFor:[self item]];
self.imageButton.userInteractionEnabled = YES;
images = nil;
});
Each time I call this function, memory increases by 10 MB. What is the good way to play animated image views?
Each time you call this method, you add a new image view (with its array of images) to your view. You should refactor your code so that the creation of the array and imageView are inside an if clause testing whether imageView is nil, so that part only runs the first time the method is called (and make imageView a property).
I have made an app, but on ipad 1 it crashes in the end part. On ipad-higher it remains working, but still I think I am doing something wrong, I really need to make something working more economically. But what is it?
In the end part it all happens. Before that, several screens are put onto each other and in the simulator when you go back they all are dismissed. But on the ipad 1 you will not even reach the end of the end part. I will try to explain what happens and several idea's about what is happening I have.
You can swipe on the screen and retina images (big images) are exchanged with the four corners that can get selected, each with a different image-background. There are six different scenes, so there are 6x4 + 6x1 for-normal-0-situation, 25 big retina images. There are only 5 frames working on top of each other (4 corners and 1 background), but I set inactive stuff to nil and load active images with imageNamed. It goes well on the simulator, but it crashes on the ipad 1, with no low memory warning or what so ever.
I've studied improvements: a) autorelease pool b) imageNamed replaced by imageContentsOfFile.
The autorelease pools don't have any effect. imageNamed, I couldn't get imageContentsOfFile working, so I skipped the operation.
Any ideas of what is so excessively wrong and what kind of thoughts can be applied to have it work more economically, for it somewhere is demanding way too much in the end.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Get the subviews of the view
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
-(void) modifyChoice:(UIView *)subview
{
if(subview==area1LinksBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:1]];}
if(subview==area2RechtsBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:2]];}
if(subview==area3RechtsOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:3]];}
if(subview==area4LinksOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:4]];}
if(maxChoice<dimensie){maxChoice=dimensie;
if(maxChoice==1){headerGevuld.image = [UIImage imageNamed:#"TOP1.png"];}
if(maxChoice==2){headerGevuld.image = [UIImage imageNamed:#"TOP2.png"];}
if(maxChoice==3){headerGevuld.image = [UIImage imageNamed:#"TOP3.png"];}
if(maxChoice==4){headerGevuld.image = [UIImage imageNamed:#"TOP4.png"];}
if(maxChoice==5){headerGevuld.image = [UIImage imageNamed:#"TOP5.png"];}
if(maxChoice==6){headerGevuld.image = [UIImage imageNamed:#"TOP6.png"];}
}
}
-(void) checkAndChange:(UIView *)subview{
if(subview==area1LinksBoven){
area1LinksBoven.image = [UIImage imageNamed:#"SELECTION1.png"];
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT1.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"1PassiefActiefOntspanning.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"1MakkelijkMoeilijkMaster.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"1VeiligGevaarlijkGeborgenheid.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"1NormaalBijzonderGeaard.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"1AlleenSamenUniek.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"1OrdeChaosDuidelijk.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area2RechtsBoven){
area1LinksBoven.image = nil;
area2RechtsBoven.image = [UIImage imageNamed:#"SELECTION2.png"];
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT2.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"2PassiefActiefEnergiek.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"2MakkelijkMoeilijkUitdagend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"2VeiligGevaarlijkSpannend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"2NormaalBijzonderWow.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"2AlleenSamenGezellig.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"2OrdeChaosImproviseren.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area3RechtsOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = [UIImage imageNamed:#"SELECTION3.png"];
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT3.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"3PassiefActiefUitputting.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"3MakkelijkMoeilijkFrustrerend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"3VeiligGevaarlijkGevaarlijk.jpg"];}
if(dimensie==4){
filmAchtergrond0.image = [UIImage imageNamed:#"3NormaalBijzonderOnbegrijjpelijk.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"3AlleenSamenGroepsdruk.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"3OrdeChaosRommeltje.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area4LinksOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = [UIImage imageNamed:#"SELECTION4.png"];
footer.image = [UIImage imageNamed:#"FOOT4.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"4PassiefActiefApathie.jpg"];}
if(dimensie==2){filmAchtergrond0.image = [UIImage imageNamed:#"4MakkelijkMoeilijkSaai.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"4VeiligGevaarlijkBeklemmend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"4NormaalBijzonderSleur.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"4AlleenSamenEenzaam.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"4OrdeChaosRigide.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(dimensie==1){headerPositie.image = [UIImage imageNamed:#"OUT1.png"];}
if(dimensie==2){headerPositie.image = [UIImage imageNamed:#"OUT2.png"];}
if(dimensie==3){headerPositie.image = [UIImage imageNamed:#"OUT3.png"];}
if(dimensie==4){headerPositie.image = [UIImage imageNamed:#"OUT4.png"];}
if(dimensie==5){headerPositie.image = [UIImage imageNamed:#"OUT5.png"];}
if(dimensie==6){headerPositie.image = [UIImage imageNamed:#"OUT6.png"];}
}
Load the images using UIImage imageWithContentsOfFile:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileExtension"];
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
See here the difference between imageNamed and imageWithContentsOfFile:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html
I'm trying to free up my apps memory and want to completely remove loadingImageView from memory when I tap a button. How do I do this?
-(IBAction)animationOneStart {
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:88];
for(int count = 1; count <= 88; count++)
{
NSString *fileName = [NSString stringWithFormat:#"testPic1_%03d.jpg", count];
UIImage *frame = [UIImage imageNamed:fileName];
[images addObject:frame];
}
loadingImageView.animationImages = images;
loadingImageView.animationDuration = 5;
loadingImageView.animationRepeatCount = 1; //Repeats indefinitely
[loadingImageView startAnimating];
[images release];
}
-(IBAction)removeFromMemory {
//What do I add here?
}
Thanks!
Ok. If you want to remove UIImageView animation try this:
-(IBAction)removeFromMemory {
[loadingImageView stopAnimating]; //here animation stops
[loadingImageView removeFromSuperview]; // here view removes from view hierarchy
[loadingImageView release]; loadingImageView = nil; //clean memory
}
For remove CoreAnimation from UIView instance use method:
[view.layer removeAllAnimations];
Hey guys,
So.... lets say I have an NSArray of images
NSMutableArray *images = [NSMutableArray new];
[images addObject:[UIImage imageNamed:#"line1.png"]];
[images addObject:[UIImage imageNamed:#"line2.png"]];
[images addObject:[UIImage imageNamed:#"line3.png"]];
[images addObject:[UIImage imageNamed:#"line4.png"]];
Now I would like to load all these at once using a for loop but here is the catch.... I need to be able to set the images as hidden until the user unhides through interaction.
for (UIImage *image in images) {
UIImageView *line = [[UIImageView alloc] initWithImage:image];
line.hidden = YES;
[self.view addSubview:line];
}
But then how to I set the hidden BOOL to NO using another method?
As a secondary question, how would one release *line in the code above?
Thanks,
Darren
One option is to set up your images like:
int nextTag = 1;
for (UIImage *image in images) {
UIImageView *line = [[UIImageView alloc] initWithImage:image];
line.hidden = YES;
line.tag = nextTag;
[self.view addSubview:line];
[line release];
nextTag++;
}
...and then to unhide them you can do:
UIView* imageView = [self.view viewWithTag: lineNumber];
imageView.hidden = NO;
...assuming that your user-interaction handler is able to determine what line in the UI the user is interacting with.
As a secondary question, how would one release *line in the code above?
for (UIImage *image in images) {
UIImageView *line = [[UIImageView alloc] initWithImage:image];
line.hidden = YES;
[self.view addSubview:line]; // this retains the subview.
[line release]; // release line like this.
}
**
-(IBAction)btnReviewStar:(id)sender{
for (int i =([sender tag] ==30); i<36; i++) {
btnReviewStar.selected = NO;
btnReviewStar1.selected = NO;
btnReviewStar2.selected = NO;
btnReviewStar3.selected = NO;
btnReviewStar4.selected = NO;
if([sender tag] == 31) {
btnReviewStar.selected = YES;
break;
} else if([sender tag]==32) {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
break;
} else if([sender tag]==33) {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
btnReviewStar2.selected = YES;
break;
} else if([sender tag]==34) {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
btnReviewStar2.selected = YES;
btnReviewStar3.selected = YES;
break;
} else {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
btnReviewStar2.selected = YES;
btnReviewStar3.selected = YES;
btnReviewStar4.selected = YES;
break;
}
}
}
**