I have a UIbutton which i want to work as a joystick. So i am trying to add some gesture recognizer on the same button.
I have this in my code right now:
#implementation CUETutorialSixteenClusterRootController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:#"CUETutorialLandscapeClusterRootController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (id)init {
self = [super init];
if (self) {
_leftInActiveView = [[NSMutableArray alloc] init];
_rightInActiveViews = [[NSMutableArray alloc] init];
_centerInActiveViews = [[NSMutableArray alloc]init];
_centerActiveViews = [[NSMutableArray alloc]init];
UIImageView * inActLeftView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActLeftView.image = [UIImage imageNamed:#"1-am-station_01.png"];
[_leftInActiveView addObject:inActLeftView];
UIImageView * inActRightView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActRightView.image = [UIImage imageNamed:#"1-am-station_03.png"];
[_rightInActiveViews addObject:inActRightView];
NSArray * imagesName = [NSArray arrayWithObjects:[UIImage imageNamed:#"1_start.png"],
[UIImage imageNamed:#"2_apptray_audio_selected.png"],
[UIImage imageNamed:#"2_apptray_phone_selected.png"],
[UIImage imageNamed:#"4_phonemenu_contacts_highlighted.png"],
[UIImage imageNamed:#"5_phonemenu_recentcalls_highlighted.png"],
[UIImage imageNamed:#"6_recent_calls.png"],
[UIImage imageNamed:#"7_bottom_hit.png"],
[UIImage imageNamed:#"8_recent_call_details.png"],
[UIImage imageNamed:#"9_calling.png"],
[UIImage imageNamed:#"9_calling_myphone.png"],
[UIImage imageNamed:#"9_call_ended.png"],nil];
NSLog(#"Count:%d",[imagesName count]);
for (int i =0; i<11; i++) {
UIImageView * inActCenterView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 226, 98)] autorelease];
inActCenterView.image = [imagesName objectAtIndex:i];
[_centerActiveViews addObject:inActCenterView];
[_centerInActiveViews addObject:inActCenterView];
}
}
return self;
}
-(void)showClusterType:(clusterType)cluster{
switch (cluster) {
//Base Cluster is analog
case kClusterTypeBase:
{
[self.fullClusterContainerView removeFromSuperview];
[self.view addSubview:self.baseClusterContainerView];
[self.view addSubview:self.baseClusterImageView];
CUETutorialSixteenAplicationHomeScreen * clusAppScreen = [[CUETutorialSixteenAplicationHomeScreen alloc] initWithDelegate:self];
clusAppScreen.dataSource = self;
clusAppScreen.rootController = self;
self.clusterHomeScreen = clusAppScreen;
[self.baseClusterContainerView addSubview:clusAppScreen.view];
[clusAppScreen release];
/*
[self.view bringSubviewToFront:self.steeringWheelImageView];
[self.view bringSubviewToFront:self.promptLabel];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavDownButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavUpButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonUp]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonLeft]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonRight]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonDown]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonCenter]];
*/
[self.view bringSubviewToFront:self.promptLabel];
}
break;
//Full cluster is digital
case kClusterTypeFull:
{
[self.baseClusterImageView removeFromSuperview];
[self.baseClusterContainerView removeFromSuperview];
[self.view addSubview:self.fullClusterContainerView];
}
break;
default:
break;
}
}
- (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.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
_step =1;
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
time=0;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)dealloc{
[super dealloc];
[_leftInActiveView release];
[_rightInActiveViews release];
[_centerActiveViews release];
[_centerInActiveViews release];
}
-(void)animateToView:(UIView *)aview{
if(aview){
//CGRect rect = [aview convertRect:aview.bounds toView:self.view];
CGRect rect = aview.frame;
[self animateHandFromRect:self.promptLabel.frame toRect:rect];
}
else{
[self animateHandToNSValueRect:nil];
}
}
-(void)finishTutorial{
[self displayPromptText:#"TUTORIAL COMPLETE"];
[self displayNavigationBarWithTitle:#"TUTORIAL COMPLETE"];
[self performBlock:^{
[self.clusterHomeScreen.view removeFromSuperview];
[self showScoresScreen];
}afterDelay:1];
}
-(void)animateHandToCenterButton
{
[self displayPromptText:#"TAP RIGHT SIDE CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE CENTER BUTTON"];
[self animateToView:self.rCenterButton];
}
-(void)animateHandToDownButton {
[self displayPromptText:#"TAP RIGHT SIDE DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE DOWN BUTTON"];
[self animateToView:self.rDownButton];
}
-(void)animateHandToUpButton{
[self displayPromptText:#"TAP RIGHT SIDE UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE UP BUTTON"];
[self animateToView:self.rUpButton];
}
-(void)replaceCallScreen{
[self performBlock:^{
[self pressedRightButtonCenter];
} afterDelay:2.0];
}
-(void)changeTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(startTicking) userInfo:nil repeats:YES];
}
-(void)startTicking {
time = time +1;
self.clusterHomeScreen.callTimer.text = [NSString stringWithFormat:#"00:0%d",time];
if (time==9) {
[timer invalidate];
}
}
-(void) animateHandToVolUpButton {
[self displayPromptText:#"TAP VOLUME UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME UP BUTTON"];
[self animateToView:self.rVolUpButton];
}
-(void) animateHandToVolDownButton {
[self displayPromptText:#"TAP VOLUME DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME DOWN BUTTON"];
[self animateToView:self.rVolDownButton];
}
-(void)removeVolumeView {
[self pressedRightButtonCenter];
[self.clusterHomeScreen runNextStep];
}
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.%#",recognizer);
}
-(IBAction)buttonClicked:(id)sender
{
if ([sender tag]==kRightButtonDown && _step ==1)
{
[self pressedRightButtonDown];
[self displayPromptText:#"TAP AGAIN"];
[self displayNavigationBarWithTitle:#"TAP AGAIN"];
_step++;
}
else if ([sender tag]==kRightButtonDown && _step == 2)
{
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step == 3)
{
[self pressedRightButtonCenter];
[self animateToView:self.rUpButton];
_step++;
}
else if ([sender tag]==kRightButtonUp && _step == 4)
{
[self pressedRightButtonUp];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
[self animateToView:self.rCenterButton];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==5){
[self pressedRightButtonCenter];
[self displayPromptText:#"TAP DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP DOWN BUTTON"];
[self animateToView:self.rDownButton];
_step++;
}
else if([sender tag]==kRightButtonDown && _step==6){
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==7){
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==8)
{
[self pressedRightButtonCenter];
self.clusterHomeScreen.callTimer.hidden = NO;
[self.clusterHomeScreen runNextStep];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==9)
{
self.clusterHomeScreen.callTimer.hidden = YES;
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==10)
{
[self pressedRightButtonCenter];
[self finishTutorial];
}
else {
[self displayVisualFeedback:kVisualFeedBackIncorrectAnswer];
}
}
#pragma CUEClusterHomeSreenDataSource Methods
-(NSInteger)numberOfViewsForLeftScreen {
return [_leftActiveViews count];
}
-(NSInteger)numberOfViewsForRightScreen{
return [_rightActiveViews count];
}
-(NSInteger)numberOfViewsForCenterScreen {
return [_centerActiveViews count];
}
-(UIImageView *)inActiveViewForLeftScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_leftInActiveView objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForLeftScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_leftActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightInActiveViews count]) && (index >=0)){
return [_rightInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightActiveViews count]) && (index >=0)){
return [_rightActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForCenterScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_centerInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForCenterScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_centerActiveViews objectAtIndex:index];
//}
//return nil;
}
This is the error i am getting now:
-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0
2012-03-28 13:25:55.724 CUETrainer[1788:11f03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0'
But its not actually doing anything.
Please help!!!!
The error says unrecognized selector sent to instance 0x79b71b0' most probably the names in .h and .m are different..
The target of the gesture recognizer needs to be the object that implements the selector. What's happening now when a swipe happens is that the gesture recognizer is doing [self.gestureReceiverButton handleSwipeFrom:recognizer]
You probably want this:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
Change every instance of:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
to
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
Problem solved. Be very careful with your #selectors, what you place in this macro is NOT validated by the compiler. Code complete will help you with this.
Also you are adding four gesture recognizers to one view, while you can accomplish the same task with one. You can combine the valid directions using the bitwise OR operator, like so:
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown)];
Related
I am adding a contact picker in my app, however, I do not want the search functionality.
How to hide/Remove the search bar on Contact Picker (ABPeoplePickerNavigationController)?
static BOOL foundSearchBar = NO;
- (void)findSearchBar:(UIView*)parent mark:(NSString*)mark {
for( UIView* v in [parent subviews] ) {
//if( foundSearchBar ) return;
NSLog(#"%#%#",mark,NSStringFromClass([v class]));
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
v.hidden=YES;
// foundSearchBar = YES;
break;
}
if( [v isKindOfClass:[UITableView class]] ) {
CGRect temp =v.frame;
temp.origin.y=temp.origin.y-44;
temp.size.height=temp.size.height+44;
v.frame=temp;
//foundSearchBar = YES;
break;
}
[self findSearchBar:v mark:[mark stringByAppendingString:#"> "]];
}
}
call above method after picker is presented as below:
-(void)showPeoplePickerController
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty],[NSNumber numberWithInt:kABPersonAddressProperty],nil];
picker.displayedProperties = displayedItems;
// Show the picker
[self presentViewController:picker animated:YES completion:nil];
[self findSearchBar:[picker view] mark:#"> "];
[picker release];
}
-(void)showAddressBook {
ABPeoplePickerNavigationController *addressBook = [[ABPeoplePickerNavigationController alloc] init];
[addressBook setPeoplePickerDelegate:self];
addressBook.delegate = self;
addressBook.navigationBar.topItem.title = #"iPhone Contacts";
UIView *view = addressBook.topViewController.view;
for (UIView *v in view.subviews) {
if ( [v isKindOfClass:[UITableView class]] ) {
CGRect temp = v.frame;
temp.origin.y = temp.origin.y - 44;
temp.size.height = temp.size.height + 44;
v.frame = temp;
}
}
[addressBook release];
}
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]]) {
UISearchDisplayController *searchDisplayController = navigationController.topViewController.searchDisplayController;
[searchDisplayController.searchBar setHidden:YES];
}
}
I implemented an app Images in full screen are displayed fine.
After few seconds the navigation bar and status bar are hidden, now if i close the app and again open it, the navigation bar is displaced at the top of the screen where status bar overlaps on navigation bar
I guess i have to change something about the CGRect frame
Please help me
#import "KTPhotoScrollViewController.h"
#import "KTPhotoBrowserDataSource.h"
#import "KTPhotoBrowserGlobal.h"
#import "KTPhotoView.h"
const CGFloat ktkDefaultPortraitToolbarHeight = 44;
const CGFloat ktkDefaultLandscapeToolbarHeight = 33;
const CGFloat ktkDefaultToolbarHeight = 44;
#define BUTTON_DELETEPHOTO 0
#define BUTTON_CANCEL 1
#interface KTPhotoScrollViewController (KTPrivate)
- (void)setCurrentIndex:(NSInteger)newIndex;
- (void)toggleChrome:(BOOL)hide;
- (void)startChromeDisplayTimer;
- (void)cancelChromeDisplayTimer;
- (void)hideChrome;
- (void)showChrome;
- (void)swapCurrentAndNextPhotos;
- (void)nextPhoto;
- (void)previousPhoto;
- (void)toggleNavButtons;
- (CGRect)frameForPagingScrollView;
- (CGRect)frameForPageAtIndex:(NSUInteger)index;
- (void)loadPhoto:(NSInteger)index;
- (void)unloadPhoto:(NSInteger)index;
- (void)trashPhoto;
- (void)exportPhoto;
#end
#implementation KTPhotoScrollViewController
#synthesize statusBarStyle = statusBarStyle_;
#synthesize statusbarHidden = statusbarHidden_;
#synthesize my_img, imgURL;
- (void)dealloc
{
[nextButton_ release], nextButton_ = nil;
[previousButton_ release], previousButton_ = nil;
[scrollView_ release], scrollView_ = nil;
[toolbar_ release], toolbar_ = nil;
[photoViews_ release], photoViews_ = nil;
[dataSource_ release], dataSource_ = nil;
[super dealloc];
}
- (id)initWithDataSource:(id <KTPhotoBrowserDataSource>)dataSource andStartWithPhotoAtIndex:(NSUInteger)index
{
if (self = [super init]) {
startWithIndex_ = index;
dataSource_ = [dataSource retain];
// Make sure to set wantsFullScreenLayout or the photo
// will not display behind the status bar.
[self setWantsFullScreenLayout:YES];
BOOL isStatusbarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
[self setStatusbarHidden:isStatusbarHidden];
self.hidesBottomBarWhenPushed = YES;
}
return self;
}
- (void)loadView
{
[super loadView];
CGRect scrollFrame = [self frameForPagingScrollView];
UIScrollView *newView = [[UIScrollView alloc] initWithFrame:scrollFrame];
[newView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[newView setDelegate:self];
UIColor *backgroundColor = [dataSource_ respondsToSelector:#selector(imageBackgroundColor)] ?
[dataSource_ imageBackgroundColor] : [UIColor blackColor];
[newView setBackgroundColor:backgroundColor];
[newView setAutoresizesSubviews:YES];
[newView setPagingEnabled:YES];
[newView setShowsVerticalScrollIndicator:NO];
[newView setShowsHorizontalScrollIndicator:NO];
[[self view] addSubview:newView];
scrollView_ = [newView retain];
[newView release];
nextButton_ = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"nextIcon.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(nextPhoto)];
previousButton_ = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"previousIcon.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(previousPhoto)];
UIBarButtonItem *msgButton = nil;
UIBarButtonItem *exportButton = nil;
exportButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(exportPhoto)];
msgButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self
action:#selector(msgPhoto)];
// UIImage *image = [UIImage imageNamed:#"Icon-Small"];
// UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
// myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
// [myMuteButton setImage:image forState:UIControlStateNormal];
// [myMuteButton addTarget:self action:#selector(trashPhoto) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];
UIBarItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSMutableArray *toolbarItems = [[NSMutableArray alloc] initWithCapacity:7];
if (exportButton) [toolbarItems addObject:exportButton];
[toolbarItems addObject:space];
[toolbarItems addObject:previousButton_];
[toolbarItems addObject:space];
[toolbarItems addObject:nextButton_];
[toolbarItems addObject:space];
if (msgButton) [toolbarItems addObject:msgButton];
// [toolbarItems addObject:myMuteBarButtonItem];
// [myMuteBarButtonItem release];
CGRect screenFrame = [[UIScreen mainScreen] bounds];
CGRect toolbarFrame = CGRectMake(0,
screenFrame.size.height - ktkDefaultToolbarHeight,
screenFrame.size.width,
ktkDefaultToolbarHeight);
toolbar_ = [[UIToolbar alloc] initWithFrame:toolbarFrame];
[toolbar_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin];
[toolbar_ setBarStyle:UIBarStyleBlackTranslucent];
[toolbar_ setItems:toolbarItems];
[[self view] addSubview:toolbar_];
if (msgButton) [msgButton release];
if (exportButton) [exportButton release];
[toolbarItems release];
[space release];
}
- (void) ShowAlert:(NSString*)title MyMsg:(NSString*)msg{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert autorelease];
}
- (void)setTitleWithCurrentPhotoIndex
{
NSString *formatString = NSLocalizedString(#"%1$i of %2$i", #"Picture X out of Y total.");
NSString *title = [NSString stringWithFormat:formatString, currentIndex_ + 1, photoCount_, nil];
[self setTitle:title];
}
- (void)scrollToIndex:(NSInteger)index
{
CGRect frame = scrollView_.frame;
frame.origin.x = frame.size.width * index;
frame.origin.y = 0;
[scrollView_ scrollRectToVisible:frame animated:NO];
}
- (void)setScrollViewContentSize
{
NSInteger pageCount = photoCount_;
if (pageCount == 0) {
pageCount = 1;
}
CGSize size = CGSizeMake(scrollView_.frame.size.width * pageCount,
scrollView_.frame.size.height / 2); // Cut in half to prevent horizontal scrolling.
[scrollView_ setContentSize:size];
}
- (void)viewDidLoad
{
[super viewDidLoad];
photoCount_ = [dataSource_ numberOfPhotos];
[self setScrollViewContentSize];
// Setup our photo view cache. We only keep 3 views in
// memory. NSNull is used as a placeholder for the other
// elements in the view cache array.
photoViews_ = [[NSMutableArray alloc] initWithCapacity:photoCount_];
for (int i=0; i < photoCount_; i++) {
[photoViews_ addObject:[NSNull null]];
}
}
- (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)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// The first time the view appears, store away the previous controller's values so we can reset on pop.
UINavigationBar *navbar = [[self navigationController] navigationBar];
if (!viewDidAppearOnce_) {
viewDidAppearOnce_ = YES;
navbarWasTranslucent_ = [navbar isTranslucent];
statusBarStyle_ = [[UIApplication sharedApplication] statusBarStyle];
}
// Then ensure translucency. Without it, the view will appear below rather than under it.
[navbar setTranslucent:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
// Set the scroll view's content size, auto-scroll to the stating photo,
// and setup the other display elements.
[self setScrollViewContentSize];
[self setCurrentIndex:startWithIndex_];
[self scrollToIndex:startWithIndex_];
[self setTitleWithCurrentPhotoIndex];
[self toggleNavButtons];
[self startChromeDisplayTimer];
}
- (void)viewWillDisappear:(BOOL)animated
{
// Reset nav bar translucency and status bar style to whatever it was before.
UINavigationBar *navbar = [[self navigationController] navigationBar];
[navbar setTranslucent:navbarWasTranslucent_];
[[UIApplication sharedApplication] setStatusBarStyle:statusBarStyle_ animated:YES];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[self cancelChromeDisplayTimer];
[super viewDidDisappear:animated];
}
- (void)deleteCurrentPhoto
{
if (dataSource_) {
// TODO: Animate the deletion of the current photo.
NSInteger photoIndexToDelete = currentIndex_;
[self unloadPhoto:photoIndexToDelete];
[dataSource_ deleteImageAtIndex:photoIndexToDelete];
photoCount_ -= 1;
if (photoCount_ == 0) {
[self showChrome];
[[self navigationController] popViewControllerAnimated:YES];
} else {
NSInteger nextIndex = photoIndexToDelete;
if (nextIndex == photoCount_) {
nextIndex -= 1;
}
[self setCurrentIndex:nextIndex];
[self setScrollViewContentSize];
}
}
}
- (void)toggleNavButtons
{
[previousButton_ setEnabled:(currentIndex_ > 0)];
[nextButton_ setEnabled:(currentIndex_ < photoCount_ - 1)];
}
#pragma mark -
#pragma mark Frame calculations
#define PADDING 20
- (CGRect)frameForPagingScrollView
{
CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index
{
CGRect bounds = [scrollView_ bounds];
CGRect pageFrame = bounds;
pageFrame.size.width -= (2 * PADDING);
pageFrame.origin.x = (bounds.size.width * index) + PADDING;
return pageFrame;
}
#pragma mark -
#pragma mark Photo (Page) Management
- (void)loadPhoto:(NSInteger)index
{
if (index < 0 || index >= photoCount_) {
return;
}
id currentPhotoView = [photoViews_ objectAtIndex:index];
if (NO == [currentPhotoView isKindOfClass:[KTPhotoView class]]) {
// Load the photo view.
CGRect frame = [self frameForPageAtIndex:index];
KTPhotoView *photoView = [[KTPhotoView alloc] initWithFrame:frame];
[photoView setScroller:self];
[photoView setIndex:index];
[photoView setBackgroundColor:[UIColor clearColor]];
// Set the photo image.
if (dataSource_) {
if ([dataSource_ respondsToSelector:#selector(imageAtIndex:photoView:)] == NO) {
UIImage *image = [dataSource_ imageAtIndex:index];
[photoView setImage:image];
} else {
[dataSource_ imageAtIndex:index photoView:photoView];
}
}
[scrollView_ addSubview:photoView];
[photoViews_ replaceObjectAtIndex:index withObject:photoView];
[photoView release];
} else {
// Turn off zooming.
[currentPhotoView turnOffZoom];
}
}
- (void)unloadPhoto:(NSInteger)index
{
if (index < 0 || index >= photoCount_) {
return;
}
id currentPhotoView = [photoViews_ objectAtIndex:index];
if ([currentPhotoView isKindOfClass:[KTPhotoView class]]) {
[currentPhotoView removeFromSuperview];
[photoViews_ replaceObjectAtIndex:index withObject:[NSNull null]];
}
}
- (void)setCurrentIndex:(NSInteger)newIndex
{
currentIndex_ = newIndex;
if(newIndex>=0){
myUrl = [dataSource_ imageURLAtIndex:currentIndex_ photoView:[photoViews_ objectAtIndex:currentIndex_]];
myDescr = [dataSource_ imageDESCRAtIndex:currentIndex_ photoView:[photoViews_ objectAtIndex:currentIndex_]];
img_Title =[dataSource_ imageimg_TitleAtIndex:currentIndex_ photoView:[photoViews_ objectAtIndex:currentIndex_]];
}
[self loadPhoto:currentIndex_];
[self loadPhoto:currentIndex_ + 1];
[self loadPhoto:currentIndex_ - 1];
[self unloadPhoto:currentIndex_ + 2];
[self unloadPhoto:currentIndex_ - 2];
[self setTitleWithCurrentPhotoIndex];
[self toggleNavButtons];
}
#pragma mark -
#pragma mark Rotation Magic
- (void)updateToolbarWithOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGRect toolbarFrame = toolbar_.frame;
if ((interfaceOrientation) == UIInterfaceOrientationPortrait || (interfaceOrientation) == UIInterfaceOrientationPortraitUpsideDown) {
toolbarFrame.size.height = ktkDefaultPortraitToolbarHeight;
} else {
toolbarFrame.size.height = ktkDefaultLandscapeToolbarHeight+1;
}
toolbarFrame.size.width = self.view.frame.size.width;
toolbarFrame.origin.y = self.view.frame.size.height - toolbarFrame.size.height;
toolbar_.frame = toolbarFrame;
}
- (void)layoutScrollViewSubviews
{
[self setScrollViewContentSize];
NSArray *subviews = [scrollView_ subviews];
for (KTPhotoView *photoView in subviews) {
CGPoint restorePoint = [photoView pointToCenterAfterRotation];
CGFloat restoreScale = [photoView scaleToRestoreAfterRotation];
[photoView setFrame:[self frameForPageAtIndex:[photoView index]]];
[photoView setMaxMinZoomScalesForCurrentBounds];
[photoView restoreCenterPoint:restorePoint scale:restoreScale];
}
// adjust contentOffset to preserve page location based on values collected prior to location
CGFloat pageWidth = scrollView_.bounds.size.width;
CGFloat newOffset = (firstVisiblePageIndexBeforeRotation_ * pageWidth) + (percentScrolledIntoFirstVisiblePage_ * pageWidth);
scrollView_.contentOffset = CGPointMake(newOffset, 0);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
// here, our pagingScrollView bounds have not yet been updated for the new interface orientation. So this is a good
// place to calculate the content offset that we will need in the new orientation
CGFloat offset = scrollView_.contentOffset.x;
CGFloat pageWidth = scrollView_.bounds.size.width;
if (offset >= 0) {
firstVisiblePageIndexBeforeRotation_ = floorf(offset / pageWidth);
percentScrolledIntoFirstVisiblePage_ = (offset - (firstVisiblePageIndexBeforeRotation_ * pageWidth)) / pageWidth;
} else {
firstVisiblePageIndexBeforeRotation_ = 0;
percentScrolledIntoFirstVisiblePage_ = offset / pageWidth;
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self layoutScrollViewSubviews];
// Rotate the toolbar.
[self updateToolbarWithOrientation:toInterfaceOrientation];
// Adjust navigation bar if needed.
if (isChromeHidden_ && statusbarHidden_ == NO) {
UINavigationBar *navbar = [[self navigationController] navigationBar];
CGRect frame = [navbar frame];
frame.origin.y = 20;
[navbar setFrame:frame];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self startChromeDisplayTimer];
}
- (UIView *)rotatingFooterView
{
return toolbar_;
}
#pragma mark -
#pragma mark Chrome Helpers
- (void)toggleChromeDisplay
{
[self toggleChrome:!isChromeHidden_];
}
- (void)toggleChrome:(BOOL)hide
{
isChromeHidden_ = hide;
if (hide) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
}
if ( ! [self isStatusbarHidden] ) {
if ([[UIApplication sharedApplication] respondsToSelector:#selector(setStatusBarHidden:withAnimation:)]) {
[[UIApplication sharedApplication] setStatusBarHidden:hide withAnimation:NO];
} else { // Deprecated in iOS 3.2+.
id sharedApp = [UIApplication sharedApplication]; // Get around deprecation warnings.
[sharedApp setStatusBarHidden:hide animated:NO];
}
}
CGFloat alpha = hide ? 0.0 : 1.0;
// Must set the navigation bar's alpha, otherwise the photo
// view will be pushed until the navigation bar.
UINavigationBar *navbar = [[self navigationController] navigationBar];
[navbar setAlpha:alpha];
[toolbar_ setAlpha:alpha];
if (hide) {
[UIView commitAnimations];
}
if ( ! isChromeHidden_ ) {
[self startChromeDisplayTimer];
}
}
- (void)hideChrome
{
if (chromeHideTimer_ && [chromeHideTimer_ isValid]) {
[chromeHideTimer_ invalidate];
chromeHideTimer_ = nil;
}
[self toggleChrome:YES];
}
- (void)showChrome
{
[self toggleChrome:NO];
}
- (void)startChromeDisplayTimer
{
[self cancelChromeDisplayTimer];
chromeHideTimer_ = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(hideChrome)
userInfo:nil
repeats:NO];
}
- (void)cancelChromeDisplayTimer
{
if (chromeHideTimer_) {
[chromeHideTimer_ invalidate];
chromeHideTimer_ = nil;
}
}
#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
float fractionalPage = scrollView.contentOffset.x / pageWidth;
NSInteger page = floor(fractionalPage);
if (page != currentIndex_) {
[self setCurrentIndex:page];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self hideChrome];
}
#pragma mark -
#pragma mark Toolbar Actions
- (void)nextPhoto
{
[self scrollToIndex:currentIndex_ + 1];
[self startChromeDisplayTimer];
}
- (void)previousPhoto
{
[self scrollToIndex:currentIndex_ - 1];
[self startChromeDisplayTimer];
}
- (void)msgPhoto
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:img_Title message:myDescr delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *message;
NSString *title;
if (!error) {
title = #"Done";
message = #"image copied to your local gallery";
} else {
title = #"Error";
message = [error description];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
//save to gallery
UIImage *imageB = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: myUrl]]];
UIImageWriteToSavedPhotosAlbum(imageB, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
} else if (buttonIndex == 1) {
//email
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
mailComposer.toolbar.barStyle = UIBarStyleBlack;
mailComposer.title = #"Your title here";
[[mailComposer navigationBar] setTintColor:[UIColor colorWithRed:124.0/255 green:17.0/255 blue:92.0/255 alpha:1]];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setSubject:#"Look at a great image"];
[mailComposer setMessageBody:[NSString stringWithFormat:#"%#",myUrl] isHTML:NO];
UIImage *imageB = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: myUrl]]];
NSData *exportData = UIImageJPEGRepresentation(imageB ,1.0);
[mailComposer addAttachmentData:exportData mimeType:#"image/jpeg" fileName:img_Title];
[self presentModalViewController:mailComposer animated:YES];
}
//release the mailComposer as it is now being managed as the UIViewControllers modalViewController.
[mailComposer release];
} else if (buttonIndex == 2) {
//cancel
}
[self startChromeDisplayTimer];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
if (result == MFMailComposeResultFailed) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void) exportPhoto
{
if ([dataSource_ respondsToSelector:#selector(exportImageAtIndex:)])
[dataSource_ exportImageAtIndex:currentIndex_];
[self startChromeDisplayTimer];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Actions"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Save to gallery", #"Email",nil];
[actionSheet showInView:[self view]];
[actionSheet release];
}
#end
First of all, tel me if you want to show Status Bar or not.
If you dont want to show then, in nib of controller's, you can select its view, and under its properties set StatusBar to NONE, thar time it wont show status bar... and you can set vew size to (320*480) or else with status bar it will be (320*460) and 20 pixels will be reserved for status bar.
Other ways to do (without using above method)
Can hide StatusBar from info.plist also, by setting property UIStatusBarHidden property to YES. (To hide the status bar when the app launches)
Programmatically can be done, add line to appDelegate's applicationDidFinishLaunching method,
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
I have UIScrollView in stage and paging functionality enabled to move back/next.
I have added 10 subviews in UIScrollView. When I have modify the content inside the subviews, then it is not reflected in UIScrollView.
Book.m
- (void) initializeWithXML:(NSString *)XMLURLString {
NSData *xmlData;
NSString *url;
if ( ![applicationData getMode] ) {
resourceRootURL = [[applicationData getAssetsPath] stringByAppendingPathComponent:#"phone/"];
} else {
resourceRootURL = [applicationData getAssetsPath];
}
url = [resourceRootURL stringByAppendingPathComponent:XMLURLString];
//NSLog(#"Root URL...%#", url);
if ([url rangeOfString:#"http"].location != NSNotFound) {
xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
} else {
xmlData = [NSData dataWithContentsOfFile:url];
}
bookContentArray = [self grabXML:xmlData andQuery:#"//page"];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
for(int i = 0; i < [bookContentArray count]; i++) {
[viewControllers addObject:[NSNull null]];
}
if ( isTwoPage ) {
kNumberOfPages = [bookContentArray count];
} else {
kNumberOfPages = [bookContentArray count]/2;
}
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
scrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor blackColor];
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = [self contentSizeForPagingScrollView];
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
scrollView.delegate = self;
self.view = scrollView;
kNumberOfPages = [bookContentArray count];
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadPage:0];
}
- (void) loadPage:(int)number {
// Calculate which pages are visible
int firstNeededPageIndex = MAX(number-1, 0);
int lastNeededPageIndex = MIN(number+1, [viewControllers count] - 1);
//NSLog(#"%d,%d",firstNeededPageIndex,lastNeededPageIndex);
// Recycle no-longer-visible pages
for(int i = 0; i < [viewControllers count]; i++) {
ImageScrollView *page = [viewControllers objectAtIndex:i];
if ((NSNull *)page != [NSNull null]) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
//NSLog(#"removed page %d", page.index);
[page removeImages];
[page removeFromSuperview];
page = nil;
[viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
}
}
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
if ( number == 0 ) {
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
} else if ( number == [bookContentArray count]-1) {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
} else {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
}
}
- (void)loadScrollViewWithPage:(int)page {
ImageScrollView *pageController = [[ImageScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
pageController.index = page;
pageController.myDelegate = self;
pageNumber = pageControl.currentPage;
pageController.isTwoPage = isTwoPage;
if ( pageNumber == page ) {
pageController.isCurrentPage = YES;
} else {
pageController.isCurrentPage = NO;
}
[controller setImageURL:leftURL andRightURL:rightURL andPriority:0];
[scrollView addSubview:controller];
}
ImageScrollView.m
- (void) setImageURL:(NSString *)leftURL andRightURL:(NSString *)rightURL andPriority:(int)priority {
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
if ( !isTwoPage ) {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
} else {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
}
if ( isTwoPage ) {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = CGRectMake(0, 43, 512, 682);
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = CGRectMake(512, 43, 512, 682);
} else {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = imageView.frame;
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = imageView.frame;
}
leftImage.delegate = self;
rightImage.delegate = self;
if ( isFirstPage ) {
[leftImage.view setHidden:YES];
[rightImage.view setHidden:NO];
} else if ( isLastPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else if ( !isTwoPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:NO];
}
imageView.backgroundColor = [UIColor whiteColor];
[self addSubview:imageView];
[leftImage setImageURL:leftURL];
[rightImage setImageURL:rightURL];
[imageView addSubview:leftImage.view];
[imageView addSubview:rightImage.view];
}
FBPage.m
- (void) setImageURL:(NSString *)url {
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 768, 1024);
imageView.center = self.view.center;
imageView.userInteractionEnabled = TRUE;
[container addSubview:imageView];
[activityIndicator setHidden:YES];
if ([url rangeOfString:#"http"].location != NSNotFound) {
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:[NSURL URLWithString:url]];
if (cachedImage) {
[imageView setImage:cachedImage];
isImageLoaded = YES;
[self addScrollView];
} else {
[activityIndicator setHidden:NO];
[activityIndicator startAnimating];
[manager downloadWithURL:[NSURL URLWithString:url] delegate:self options:0 success:^(UIImage *image){
[activityIndicator stopAnimating];
[activityIndicator setHidden:YES];
[imageView setImage:image];
isImageLoaded = YES;
[self addScrollView];
} failure:nil];
}
imageURL = url;
} else {
imageView.image = [UIImage imageWithContentsOfFile:url];
isImageLoaded = YES;
[self addScrollView];
}
}
- (void) setHotspotURL:(NSString *)URL {
[hotspot.view removeFromSuperview];
[hotspot release];
hotspot = nil;
hotspot = [[FBHotspot alloc] initWithNibName:#"FBHotspot" bundle:nil];
hotspot.view.frame = CGRectMake(0, 0, 768, 1024);
hotspot.delegate = self;
[hotspot setURL:URL];
[container addSubview:hotspot.view];
}
**When I am calling setHotspotURL and it’s not updating to the scrollview.**
Make sure you inserted your content at the correct position within the scroll view. Set the contentSize accordingly. Try to call layoutSubviews of the UIScrollView instance
I have an app that's almost done and now while it was under QA, the QA engineer came across a random issue in which a black screen appears after tapping on USE in UIImagePickerController view. Further in didFinishPickingMediaWithInfo the image is being saved for future refrencing and is being shown in an UIImageView, I am also using camera overlay for adding a button to the UIImagePickerView and the max memory usage of the app doen't goes beyond 13 MB. The issue is not arising when switching to Camera roll mode from capture mode. Also in the same view iADs are being presented. Underneath is the code, to which the issue may concern, also images are being attached to get an idea of the issue. Any help would be greatly appreciated.
Thanks in advance.
(void) launchCamera
{
// Set up the camera\
CustomCameraView *cameraController = [[CustomCameraView alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.delegate = self;
cameraController.showsCameraControls = YES;
cameraController.navigationBarHidden = YES;
cameraController.toolbarHidden = YES;
// overlay on top of camera lens view
UIButton *cameraRoll = [[UIButton alloc] initWithFrame:CGRectMake(15, 380, 40, 40)];
[cameraRoll setAlpha:0.0f];
[cameraRoll setBackgroundImage:[UIImage imageNamed:#"CamerRoll_New"] forState:UIControlStateNormal];
[cameraRoll addTarget:self action:#selector(switchToCameraRoll) forControlEvents:UIControlEventTouchUpInside];
cameraController.cameraOverlayView = cameraRoll;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:1.5f];
cameraRoll.alpha = 1.0f;
[UIView commitAnimations];
[self presentModalViewController:cameraController animated:YES];
}
-(void)switchToCameraRoll
{
DLog(#"Camera Roll");
[self dismissViewControllerAnimated:NO completion:^(void){ [self photoSourcePhotoAlbum];}];
}
-(void) photoSourcePhotoAlbum
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.view addSubview:progressView];
timer = [NSTimer scheduledTimerWithTimeInterval:.017 target:self selector:#selector(progressChange) userInfo:nil repeats:YES];
[picker dismissModalViewControllerAnimated:YES];
[self performSelectorInBackground:#selector(saveImage:) withObject:info];
[self performSelector:#selector(navigateToEditView) withObject:nil afterDelay:2.1];
}
-(void)navigateToEditView
{
[self performSegueWithIdentifier:#"presentRDetailModalViewController" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum)
{
[picker dismissViewControllerAnimated:NO completion:^(void){ [self photoSourceCamera];}];
}
else
{
[picker dismissModalViewControllerAnimated:YES];
}
}
-(void)saveImage:(NSDictionary *)info
{
NSString *imageName = [[[DataStaging dataStaging] getGUID] stringByAppendingString:#".jpg"];
rImageName = imageName;
UIImage *rImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Compress image
[[FileOperations fileOperations] PersistData:UIImageJPEGRepresentation(rImage, 0.4) name:imageName];
DLog(#"%#",[[FileOperations fileOperations] fetchPath:imageName]);
//Actual image taken
[[self rImageView] setImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"]];
if ([rImageName length] == 0)
{
[[self rDetails] setHidden:YES];
[[self trashRImage] setHidden:YES];
}
else
{
[[self rDetails] setHidden:NO];
[[self trashRImage] setHidden:NO];
}
[progressView removeFromSuperview];
}
}
Make sure you don't call presentModalViewController:animated: twice before dismissModalViewControllerAnimated:
It helped in my case.
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
if (_imagePickerController!=nil || _imagePopoverController!=nil) {
return;
}
_imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
_imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
_imagePickerController.sourceType = sourceType;
_imagePickerController.delegate = self;
switch (sourceType) {
case UIImagePickerControllerSourceTypeCamera:
_imagePickerController.showsCameraControls = YES;
[self presentViewController:_imagePickerController animated:YES completion:nil];
break;
default:
_imagePopoverController = [[UIPopoverController alloc] initWithContentViewController:_imagePickerController];
_imagePopoverController.delegate = self;
CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.y = rect.size.height - 70.0;
rect.size.height -= rect.origin.y;
[_imagePopoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
break;
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[_imagePopoverController dismissPopoverAnimated:YES];
_imagePopoverController = nil;
[_imagePickerController dismissViewControllerAnimated:YES completion:nil];
_imagePickerController = nil;
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImageWriteToSavedPhotosAlbum([info objectForKey:UIImagePickerControllerOriginalImage],nil,nil,nil);
_currentImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[_imagePopoverController dismissPopoverAnimated:YES];
_imagePopoverController = nil;
[_imagePickerController dismissViewControllerAnimated:YES completion:nil];
_imagePickerController = nil;
}
I have looked at a few guides for CAKeyFrameAnimation and I am failing to see how to trigger them. The only thing I can think of is that I have to use it as a return, but that doesn't make much sense to me.
-H File-
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface ImageSequenceViewController : UIViewController
<UIGestureRecognizerDelegate>{
UISwipeGestureRecognizer *swipeLeftRecognizer;
NSMutableArray *myImages;
IBOutlet UIImageView *imageView;
IBOutlet UIImageView *bView;
IBOutlet UISegmentedControl *segmentedControl;
}
#property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeftRecognizer;
#property (nonatomic, retain) UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;
-(IBAction)takeLeftSwipeRecognitionEnabledFrom:(UISegmentedControl *)aSegmentedControl;
-(IBAction)ButtonPressed1: (id)sender;
#end
-M File-
#import "ImageSequenceViewController.h"
#implementation ImageSequenceViewController
#synthesize swipeLeftRecognizer;
#synthesize imageView;
#synthesize segmentedControl;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
//CUSTOM CODE
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)loadLeft {
//aView = [[UIImageView alloc] initWithFrame:self.view.frame];
CALayer *layer = [CALayer layer];
[layer setFrame:CGRectMake(0.0,
0.0,
[[self view] frame].size.height,
[[self view] frame].size.width)];
myImages = [[NSMutableArray alloc] init];
for(NSUInteger count=0; count<100; count++){
NSString *fileName;
if (count < 10)
{
fileName = [NSString stringWithFormat:#"trailerRotation_000%d", count];
}
else if (10 <= count < 100)
{
fileName = [NSString stringWithFormat:#"trailerRotation_00%d", count];
}
else
{
fileName = [NSString stringWithFormat:#"trailerRotation_0%d", count];
}
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"jpg"];
//UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileName];
//[myImages addObject:image];
[myImages addObject:[UIImage imageWithContentsOfFile:path]];
}
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:#"Contents"];
[anim setDuration:0.10];
[anim setCalculationMode:kCAAnimationDiscrete];
[anim setRepeatCount:1];
[anim setValues:myImages];
[self.view.layer addSublayer:layer];
[layer addAnimation:anim forKey:#"images"];
//aView.animationImages = myImages;
//aView.animationDuration = 10.00;
//aView.animationRepeatCount = 1;
}
-(void)loadRight {
bView = [[UIImageView alloc] initWithFrame:self.view.frame];
myImages = [[NSMutableArray alloc] init];
for(NSUInteger count=99; count>0; count--){
NSString *countString;
if (count < 10)
{
countString = #"000";
countString = [countString stringByAppendingFormat:#"%d", count];
}
else if (10 <= count < 100)
{
countString = #"00";
countString = [countString stringByAppendingFormat:#"%d", count];
}
else if (100 <= count < 1000)
{
countString = #"00";
countString = [countString stringByAppendingFormat:#"%d", count];
}
NSLog(#"%d", count);
NSString *fileName = #"trailerRotation_";
fileName = [fileName stringByAppendingFormat:countString];
fileName = [fileName stringByAppendingFormat:#".jpg"];
[myImages addObject:[UIImage imageNamed:fileName]];
}
bView.animationImages = myImages;
bView.animationDuration = 10.00;
bView.animationRepeatCount = 1;
}
- (void)viewDidLoad {
[super viewDidLoad];
imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageView setImage:[UIImage imageNamed:#"trailerRotation_0000.jpg"]];
//[self.view addSubview:imageView];
[self loadLeft];
[self loadRight];
UIGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
self.swipeLeftRecognizer = (UISwipeGestureRecognizer *) recognizer;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
if ([segmentedControl selectedSegmentIndex] == 0) {
[self.view addGestureRecognizer:swipeLeftRecognizer];
}
self.swipeLeftRecognizer = (UISwipeGestureRecognizer *) recognizer;
[recognizer release];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.segmentedControl = nil;
self.swipeLeftRecognizer = nil;
self.imageView = nil;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[self becomeFirstResponder];
}
-(void)startItLeft {
NSLog(#"Left");
//[aView startAnimating];
//[self.view addSubview:aView];
//[aView release];
[bView release];
}
-(void)startItRight {
NSLog(#"Right");
[bView startAnimating];
[self.view addSubview:bView];
//[aView release];
[bView release];
}
//- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//}
//- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//}
//- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//}
-(IBAction)ButtonPressed1:(id)sender
{
NSLog(#"Button");
//[aView stopAnimating];
[bView stopAnimating];
//[self loadLeft];
[self loadRight];
}
-(IBAction)takeLeftSwipeRecognitionEnabledFrom:(UISegmentedControl *) aSegmentControl {
if ([aSegmentControl selectedSegmentIndex] == 0) {
[self.view addGestureRecognizer:swipeLeftRecognizer];
}
else {
[self.view removeGestureRecognizer:swipeLeftRecognizer];
}
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *) recognizer {
//CGPoint location = [recognizer locationInView:self.view];
//[self showImageWithText:#"swipe" atPoint:location];
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
//[self startItLeft];
}
else {
[self startItRight];
}
}
//CUSTOM CODE
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (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)dealloc {
[super dealloc];
}
#end
I'm rather new to iOS development so any advice is helpful.
Thanks.
Try:
[myImages addObject:(id)[UIImage imageWithContentsOfFile:path].CGImage];
Since the CAKeyFrameAnimation expects CGImageRef objects.
It looks like you're trying to load up 100 images and cycle them all within the space of 0.1 second. Not only is that pretty ridiculous (that's 1000fps right there, and CoreAnimation is capped at 60fps), but it's likely to take longer than 0.1 seconds for CoreAnimation to copy all the images before it even starts, which means that it will have finished before it started (and therefore no animation will occur).
I'll admit it was a PEBKAC error.
Thanks all for your help.