CABasicAnimation not working with UIImageView - xcode

I have a UIImageView that is called upon programatically, I am trying to get it to spin around but its not working. The image will be placed inside a dynamic UITableView (I cannot change it to static). The image appears fine in my table view but it just doesn't spin.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)];
imageView.image = [UIImage imageNamed:#"settings1.png"];
[self.view addSubview:imageView];
CABasicAnimation *spin;
spin = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
spin.fromValue = [NSNumber numberWithFloat:0];
spin.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
spin.duration = 4;
spin.repeatCount = 10*1000;
[imageView.layer addAnimation:spin forKey:#"360"];

It's probably because you're trying to start the animation in viewDidLoad. I'd suggest reorganizing your code in such a way that you create the image view instance in viewDidLoad, but then wait until viewDidAppear: is called to actually start the animation.
#property (strong, nonatomic) UIImageView *imageView;
And then
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)];
self.imageView.image = [UIImage imageNamed:#"settings1.png"];
[self.view addSubview:self.imageView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CABasicAnimation *spin = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
spin.fromValue = [NSNumber numberWithFloat:0];
spin.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
spin.duration = 4;
spin.repeatCount = 10*1000;
[self.imageView.layer addAnimation:spin forKey:#"360"];
}

Related

addSublayer over video preview in OSX

I'm trying to add a simple text label over a video preview. The video preview works fine, but the text label is no where to be found. The compiler isn't giving me any errors or warnings either. I know I'm mixing dot notation in there, but I'm not sure if that's my root cause or not.
This is a clip from my .m file:
#interface AVRecorderDocument ()
#property (retain) AVCaptureVideoPreviewLayer *previewLayer;
#property (nonatomic, strong) NSTextField *labelVideoOverlay;
#end
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
CALayer *previewViewLayer = [[self previewView] layer];
self.labelVideoOverlay = [[NSTextField alloc] initWithFrame:CGRectMake(100, 120, 200, 40)];
self.labelVideoOverlay.stringValue = #"TEST";
self.labelVideoOverlay.backgroundColor = [NSColor blackColor];
self.labelVideoOverlay.textColor = [NSColor whiteColor ];
[previewViewLayer setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
AVCaptureVideoPreviewLayer *newPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[self session]];
[newPreviewLayer setFrame:[previewViewLayer bounds]];
[newPreviewLayer setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];
[previewViewLayer addSublayer: self.labelVideoOverlay.layer];
[previewViewLayer addSublayer:newPreviewLayer];
[self setPreviewLayer:newPreviewLayer];
[newPreviewLayer release];
}
It looks like although you've added the video preview as a sublayer, you still need to add the overlay as a subview to your main view. I did something similar like this recently (just with an UIImageView), but the principal should still be the same.
self.overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"overlaygraphic.png"]];
self.overlayImageView.translatesAutoresizingMaskIntoConstraints = NO;
[[self view] addSubview:self.overlayImageView];
So, you could do something like:
self.labelVideoOverlay = [[NSTextField alloc] initWithFrame:CGRectMake(100, 120, 200, 40)];
self.labelVideoOverlay.stringValue = #"TEST";
self.labelVideoOverlay.backgroundColor = [NSColor blackColor];
self.labelVideoOverlay.textColor = [NSColor whiteColor ];
[[self view] addSubview:self.labelVideoOverlay];

Simple way to change the position of UIView

- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
// self.view.frame=CGRectMake(0,20,self.view.frame.size.width,self.view.frame.size.height-20);
self.headerView.frame=CGRectMake(0,20,320,45);
}
but headerview not change can any one help
-(void)viewDidLoad
{
// ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:imageView];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
// ...
}

Animate image from array with animation style

I have 3 images in my array for this sample code it just animate without any style .I want to animate like fade in fade out style.
NSArray *imarray = [[NSArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/lady1_open.png", path]],
[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/lady1_open2.png", path]],
[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/lady1_open3.png", path]],
nil];
bingo_girl.animationImages=imarray;
bingo_girl.animationDuration=5;
bingo_girl.animationRepeatCount=0;
[bingo_girl.layer addAnimation:transition forKey:nil];
[bingo_girl startAnimating];
You may animate images with transition manually for example using this category
#implementation UIImageView (AnimateTransition)
- (void) assignImage:(UIImage *)image withTransition:(NSString *)withTransition withDirection:(NSString *)withDirection{
if(image == nil)
{
[self setImage:nil];
return;
}
if ([UIImagePNGRepresentation(self.image) isEqualToData:UIImagePNGRepresentation(image)]) {
return;
}
CATransition *animation = [CATransition animation];
animation.duration = 0.2;
animation.type = withDirection;
animation.subtype = withDirection;
[[self layer] addAnimation:animation forKey:#"imageFade"];
[self setImage:image];
}
#end

fading slideshow in an imageview. xCode 4.2

I am trying to make a slideshow in an imageview that uses also fade-in fade-out effects.
so far ive done this:
- (void)viewDidLoad
{
[super viewDidLoad];
animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.jpg"],
[UIImage imageNamed:#"photo2.jpg"],
[UIImage imageNamed:#"photo3.jpg"], nil];
animationView.animationDuration = 5;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
images do appear one after the other, in a 5 sec delay, what i want now is to make them fade-in and fade-out each time an image appears, any suggestions on that?
Thanks in advance for your help
I was trying to do something similar. What I did was setup a timer that called a UIView transitionWithView on a UIImageView that was incrementing through the array of my slideshow photos.
- (void)viewDidLoad{
[super viewDidLoad];
self.welcomePhotos = [NSArray arrayWithObjects:#"welcome_1.png",#"welcome_2.png", #"welcome_3.png", nil];
self.imageView1.image = [UIImage imageNamed:[self.welcomePhotos objectAtIndex:0]];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(transitionPhotos) userInfo:nil repeats:YES];
}
-(void)transitionPhotos{
if (photoCount < [self.welcomePhotos count] - 1){
photoCount ++;
}else{
photoCount = 0;
}
[UIView transitionWithView:self.imageView1
duration:2.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ self.imageView1.image = [UIImage imageNamed:[self.welcomePhotos objectAtIndex:photoCount]]; }
completion:NULL];
}

How do I implement UIScrollView programatically

I am launching detail pages from a UITableView and within that detail page there is an image. I would like it to be zoomable, so I'd like the contents of that detail page to sit inside a scrollView. So far no luck, the labels, textviews and images are fine, but I can't get the ScrollView implemented.
here is my .m: THANKS!
- (void)viewDidLoad {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.contentSize = CGSizeMake(320, 700);
scrollView.clipsToBounds = YES;
super.view.backgroundColor = [UIColor whiteColor];
[scrollView setUserInteractionEnabled:YES];
self.navigationItem.title = [NSString stringWithFormat:selectedCellItem];
CGRect frame = CGRectMake(0, 0, 10, 10);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.backgroundColor = [UIColor whiteColor];
frame = CGRectMake(0, 320, 10, 10);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
//label.text = selectedCellItem;
label.font=[UIFont fontWithName:#"Verdana-Bold" size:20.0];
label.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label];
[label release];
frame = CGRectMake(0, 320, 320, 60); //Description
UITextView *labelDesc = [[UITextView alloc] initWithFrame:frame];
labelDesc.text = selectedCellDesc;
labelDesc.font=[UIFont fontWithName:#"Verdana" size:12.0];
labelDesc.textAlignment = UITextAlignmentLeft;
//labelDesc.textAlignment = UITextAlignmentTop;
[self.view addSubview:labelDesc];
[labelDesc release];
frame = CGRectMake(0, 0, 320, 320); //Image
UIImageView *imageItem = [[UIImageView alloc] initWithFrame:frame];
[imageItem setImage:[UIImage imageNamed:selectedCellImage]];
// GET THIS ONE WORKING RIGHT>>[imageItem setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png", [[NSBundle mainBundle] bundlePath], selectedCellImage]]]; //use
//imageItem = [[UIImageView alloc] initWithContentsOfFile:[NSString stringWithFormat:#"shirt.png", [[NSBundle mainBundle] bundlePath]]];
//labelDesc.font=[UIFont fontWithName:#"Zapfino" size:13.0];
//labelDesc.textAlignment = UITextAlignmentCenter;
imageItem.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageItem];
//[imageItem release];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I think that you're missing the following line to add the UIScrollView as a subview.
[self.view addSubView:scrollView];

Resources