2012-04-10 56 views
2

嗨即时新的iPhone应用程序开发。我想使用页面控制器为我的uiscroll视图。在单一视图即时通讯使用两个滚动视图和两个页面controls.i设置pagecontrol与以下编码,但只有两页它的工作fine.but我想增加超过两页的whic由静态按键滚动视图[在这里输入的形象描述] [1]如何为uiscroll视图添加pagecontrol?

// 
// newsampleViewController.m 
// newsample 
// 
// Created by SmartJobDevelopers on 4/3/12. 
// Copyright 2012 __MyCompanyName__. All rights reserved. 
// 

#import "newsampleViewController.h" 

@implementation newsampleViewController 

@synthesize scr_anger; 
@synthesize scr_sketch; 

@synthesize pageControl; 
@synthesize pageControl1; 

/* 
// 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 { 
} 
*/ 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    [self setupPage]; 



    //[scr_anger setScrollEnabled:YES]; 
    //[scr_anger setContentSize:CGSizeMake(1290,15)]; 

    //[scr_sketch setScrollEnabled:YES]; 
    //[scr_sketch setContentSize:CGSizeMake(1310,15)]; 

    [super viewDidLoad]; 
} 

-(IBAction)buttonPressed:(id)sender { 
     i_curtag=(int)[sender tag]; 
     //NSLog(@"%d",i_curtag); 
     [self fn_btnOperation]; 
     //[self opt:str_filename]; 
     //NSLog(@"str_filename:%@",str_filename); 
    } 

-(void)fn_btnOperation 
{ 
    if (i_curtag==1) 
    { 
     //str_filename=[NSString stringWithFormat:@"%d.png",i_curtag]; 
     //NSLog(@"str_filename:%@",str_filename); 
     [email protected]"1.png"; 
     // NSLog(@"1.png"); 
    } 
    else if(i_curtag==2) 
    { 
     [email protected]"2.png"; 
     //NSLog(@"2.png"); 
    } 
    else if(i_curtag==3) 
    { 
     [email protected]"3.png"; 
     //NSLog(@"3.png"); 
    } 
    else if(i_curtag==4) 
    { 
     [email protected]"4.png"; 
     // NSLog(@"4.png"); 
    } 
    else if(i_curtag==5) 
    { 
     [email protected]"5.png"; 
     //NSLog(@"5.png"); 
    } 
    else if(i_curtag==6) 
    { 
     [email protected]"6.png"; 
     //NSLog(@"6.png"); 
    } 
    else if(i_curtag==7) 
    { 
     [email protected]"7.png"; 
     // NSLog(@"7.png"); 
    } 
    else if(i_curtag==8) 
    { 
     [email protected]"8.png"; 
     //NSLog(@"8.png"); 
    } 
    else if(i_curtag==9) 
    { 
     [email protected]"9.png"; 
     // NSLog(@"9.png"); 
    } 
    else if(i_curtag==10) 
    { 
     [email protected]"10.png"; 
     // NSLog(@"10.png"); 
    } 

    [self opt:str_filename]; 
} 

-(id)opt:(NSString*)filename 
{ 
    //NSLog(@"filename:%@",filename); 
    str_filename=filename; 
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@""delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save Image" otherButtonTitles:@"Mail Image",nil]; 
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    [popupQuery showInView:self.view]; 
    [popupQuery release]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex1 
{ 
    if (buttonIndex1 == 0) 
    { 
     // NSLog(@"str_filename in actionsheet:%@",str_filename); 
     [self savesingleimage]; 
     // NSLog(@"save"); 
    } 

    else if (buttonIndex1 == 1) 
    { 
     //NSLog(@"str_filename in else:%@",str_filename); 
     [self fmail]; 
     //NSLog(@"mail"); 
    } 
} 

-(void)fmail 
{ 
    //NSLog(@"str_filename:%@",str_filename); 

     NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body><p>"] retain]; 

    [emailBody appendString:@"<b><u>EMOTIONAL ICONS</u></b><br><br>"]; 

    [emailBody appendString:@"</p></body></html>"]; 

     MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init]; 

    UIImage *icon1 = [UIImage imageNamed:str_filename]; 
    NSData *imageData1 = UIImageJPEGRepresentation(icon1, 1); 
    [emailDialog addAttachmentData:imageData1 mimeType:@"image/jpg" fileName:str_filename]; 
     emailDialog.mailComposeDelegate =self; 
     [emailDialog setSubject:@"Emotional Icons"]; 
     [emailDialog setMessageBody:emailBody isHTML:YES]; 
     [emailDialog.navigationBar setTintColor:[UIColor blackColor]]; 

     [self presentModalViewController:emailDialog animated:YES]; 
     [emailDialog release]; 
     [emailBody release]; 
} 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status:" message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; 

    switch (result) { 
     case MFMailComposeResultCancelled: 
      alert.message = @"Message Canceled"; 
      break; 
     case MFMailComposeResultSaved: 
      alert.message = @"Message Saved"; 
      break; 
     case MFMailComposeResultSent: 
      alert.message = @"Message Sent"; 
      break; 
     case MFMailComposeResultFailed: 
      alert.message = @"Message Failed"; 
      break; 
     default: 
      alert.message = @"Message Not Sent"; 
      break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 

    [alert show]; 
    [alert release]; 
} 

-(void)savesingleimage{ 

    // NSLog(@"str_filename in save: %@",str_filename); 
    UIImage *image1 = [UIImage imageNamed:str_filename];  
    UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil); 
    UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Saved" 
     message:@"Image Saved into PhotoAlbum" 
     delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 170, 50, 50)]; 
    NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:str_filename]]; 
    UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path]; 
    [imageView setImage:bkgImg]; 
    [bkgImg release]; 
    [path release]; 

    [successAlert addSubview:imageView]; 
    [imageView release]; 

    [successAlert show]; 
    [successAlert release]; 
} 


#pragma mark - 
#pragma mark The Guts 
- (void)setupPage 
{ 
    scr_anger.delegate = self; 
    scr_sketch.delegate =self; 

    //[self.scr_anger setBackgroundColor:[UIColor blackColor]]; 
    [scr_anger setCanCancelContentTouches:NO]; 
    [scr_sketch setCanCancelContentTouches:NO]; 

    scr_anger.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    scr_anger.clipsToBounds = YES; 
    scr_anger.scrollEnabled = YES; 
    scr_anger.pagingEnabled = YES; 

    scr_sketch.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    scr_sketch.clipsToBounds = YES; 
    scr_sketch.scrollEnabled = YES; 
    scr_sketch.pagingEnabled = YES; 

    self.pageControl.numberOfPages = 2; 
    self.pageControl1.numberOfPages = 2; 


    [scr_anger setScrollEnabled:YES]; 
    [scr_sketch setScrollEnabled:YES]; 

    [scr_anger setContentSize:CGSizeMake(1290,15)]; 
    [scr_sketch setContentSize:CGSizeMake(1940,15)]; 

} 

#pragma mark - 
#pragma mark UIScrollViewDelegate stuff 
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView 
{ 
    // NSLog(@"scrollview val=%d",_scrollView.tag); 
    i=_scrollView.tag; 
    // NSLog(@"i=%d",i); 
    if (_scrollView.tag==101) { 

    if (pageControlIsChangingPage) { 
     return; 
    } 

    /* 
    * We switch page at 50% across 
    */ 

    //scr_anger.frame = CGRectMake(0, 0, 320, 0); 
    CGFloat pageWidth =scr_anger.frame.size.width; 
    //NSLog(@"pagewidth=%f",pageWidth); 
    int page = floor((scr_anger.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    // NSLog(@"page=%d",page); 
    pageControl.currentPage = page; 
     } 
    else if(_scrollView.tag==102) 
    { 
     if (pageControlIsChangingPage) { 
      return; 
     } 

     /* 
     * We switch page at 50% across 
     */ 

     CGFloat pageWidth =scr_sketch.frame.size.width; 

     int page = floor((scr_sketch.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

     pageControl1.currentPage = page; 
     NSLog(@"page=%d",page); 
    } 
} 


- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{ 
    pageControlIsChangingPage = NO; 
} 

#pragma mark - 
#pragma mark PageControl stuff 
- (IBAction)changePage:(id)sender 
{ 
    /* 
    * Change the scroll view 
    */ 
    //NSLog(@"i=%d",i); 
    if (i==101) { 


    CGRect frame = scr_anger.frame; 


    //pageControl.frame = CGRectMake(0, 390, 320, 15); 
    frame.origin.x = frame.size.width * pageControl.currentPage; 
     frame.origin.y = 0; 

    [scr_anger scrollRectToVisible:frame animated:YES]; 
    /* 
    * When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off 
    */ 
    pageControlIsChangingPage = YES; 
    } 

    else if(i==102) 
    { 
     CGRect frame1 = scr_sketch.frame; 

     frame1.origin.x = frame1.size.width * pageControl1.currentPage; 
     frame1.origin.y = 0; 

     [scr_sketch scrollRectToVisible:frame1 animated:YES]; 

     pageControlIsChangingPage = YES; 
    } 

} 



/* 
// 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 { 
    [scr_anger release]; 
    [scr_sketch release]; 

    [pageControl release]; 
    [pageControl1 release]; 
     // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 
+0

添加您的图像正确.. – 2012-04-10 13:40:47

+0

因为我新来stackoverflow我无法添加图像... – 2012-04-11 12:17:35

+0

你已添加您的图像正确。添加后,您刚刚删除空格。这使得您的图像无法使用。再次上传它,并在此之后不删除任何东西。你试过我的回答吗? – 2012-04-11 12:19:39

回答

0
+0

嗨,谢谢很多。我检查与tat tutorial.since我新开发我无法理解它fully.im使用2个滚动视图在单个view.for每个滚动视图我必须设置页面控制与多个page.its工作正常两页,但不是第三个。你能帮我吗? – 2012-04-11 12:29:04

+0

当然可以请你分享我一些进一步的信息或代码。或者请给我发邮件[email protected] – maheswaran 2012-04-12 07:06:43

0

在你setupPage:方法,你有一个代码允许这样的页数。

self.pageControl.numberOfPages = 2; 
self.pageControl1.numberOfPages = 2; 

如果您想增加一个numberOfPages,然后增加这些行中的numberOfPages。

+0

嗨thanks.i尝试过,但它不工作,在页面控制中添加页面(点),但我的问题是它没有增加我的滚动view.in滚动查看其只显示两页。 – 2012-04-11 12:24:09

+0

你必须增加scrollView ContentSize取决于页数。 – 2012-04-11 12:28:32

2

This是一个关于UIScrollView & IPageControl组合的非常好的教程。但我自己寻找更动态的设计的tutorail(如ios应用程序的主页 - 我增量 - 自动递减)

+0

嗨,谢谢很多。我检查了tat tutorial.since我新开发我无法理解它fully.im使用2个滚动视图在单个view.for每个滚动视图我必须设置页面控制与多个page.its工作正常两页,但不是第三个。你能帮我吗? – 2012-04-11 12:29:22

+0

@MythiliLakshmanan它可能是数组数的原因。你能分享一些代码或错误日志吗? – ilhnctn 2012-04-11 12:31:27

+0

hi ilis,我已经在上面的问题中分享了我的完整代码。请你帮我解决我的问题吗? – 2012-04-12 11:48:57

相关问题