菜鸟教程小白 发表于 2022-12-11 22:33:16

ios - 如何为uiscrollview添加页面控件?


                                            <p><p>嗨,我是 iphone 应用程序开发的新手。我想为我的 uiscrollView 使用页面 Controller 。在单个 View 中,我使用两个 ScrollView 和两个页面控件。我使用以下编码设置了 pagecontrol,但只有两个页面工作正常。但是我想为包含静态按钮的 ScrollView 添加两个以上的页面。![在此处输入图像描述]</p>

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

#import &#34;newsampleViewController.h&#34;

@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 = ;
    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 {

    ;



    //;
    //;

    //;
    //;

    ;
}

-(IBAction)buttonPressed:(id)sender {
      i_curtag=(int);
      //NSLog(@&#34;%d&#34;,i_curtag);
      ;
      //;
      //NSLog(@&#34;str_filename:%@&#34;,str_filename);
   }

-(void)fn_btnOperation
{
    if (i_curtag==1)
    {
      //str_filename=;
      //NSLog(@&#34;str_filename:%@&#34;,str_filename);
      str_filename=@&#34;1.png&#34;;
       // NSLog(@&#34;1.png&#34;);
    }
    else if(i_curtag==2)
    {
      str_filename=@&#34;2.png&#34;;
      //NSLog(@&#34;2.png&#34;);
    }
    else if(i_curtag==3)
    {
      str_filename=@&#34;3.png&#34;;
      //NSLog(@&#34;3.png&#34;);
    }
    else if(i_curtag==4)
    {
      str_filename=@&#34;4.png&#34;;
       // NSLog(@&#34;4.png&#34;);
    }
    else if(i_curtag==5)
    {
      str_filename=@&#34;5.png&#34;;
      //NSLog(@&#34;5.png&#34;);
    }
    else if(i_curtag==6)
    {
      str_filename=@&#34;6.png&#34;;
      //NSLog(@&#34;6.png&#34;);
    }
    else if(i_curtag==7)
    {
      str_filename=@&#34;7.png&#34;;
       // NSLog(@&#34;7.png&#34;);
    }
    else if(i_curtag==8)
    {
      str_filename=@&#34;8.png&#34;;
      //NSLog(@&#34;8.png&#34;);
    }
    else if(i_curtag==9)
    {
      str_filename=@&#34;9.png&#34;;
       // NSLog(@&#34;9.png&#34;);
    }
    else if(i_curtag==10)
    {
      str_filename=@&#34;10.png&#34;;
       // NSLog(@&#34;10.png&#34;);
    }

    ;
}

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

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex1
{
    if (buttonIndex1 == 0)
    {
       // NSLog(@&#34;str_filename in actionsheet:%@&#34;,str_filename);
      ;
       // NSLog(@&#34;save&#34;);
    }

    else if (buttonIndex1 == 1)
    {
      //NSLog(@&#34;str_filename in else:%@&#34;,str_filename);
      ;
      //NSLog(@&#34;mail&#34;);
    }
}

-(void)fmail
{   
    //NSLog(@&#34;str_filename:%@&#34;,str_filename);

      NSMutableString *emailBody = [[ initWithString:@&#34;&lt;html&gt;&lt;body&gt;&lt;p&gt;&#34;] retain];

    ;

    ;

      MFMailComposeViewController *emailDialog = [ init];

    UIImage *icon1 = ;
    NSData *imageData1 = UIImageJPEGRepresentation(icon1, 1);
    ;
      emailDialog.mailComposeDelegate =self;
      ;
      ;
      ];

      ;
      ;
      ;
}

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

    switch (result) {
      case MFMailComposeResultCancelled:
            alert.message = @&#34;Message Canceled&#34;;
            break;
      case MFMailComposeResultSaved:
            alert.message = @&#34;Message Saved&#34;;
            break;
      case MFMailComposeResultSent:
            alert.message = @&#34;Message Sent&#34;;
            break;
      case MFMailComposeResultFailed:
            alert.message = @&#34;Message Failed&#34;;
            break;
      default:
            alert.message = @&#34;Message Not Sent&#34;;
            break;
    }
    ;

    ;
    ;
}

-(void)savesingleimage{

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

    ;
    ;

    ;
    ;
}


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

    //];
    ;
    ;

    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;


    ;
    ;

    ;
    ;

}

#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
   // NSLog(@&#34;scrollview val=%d&#34;,_scrollView.tag);
    i=_scrollView.tag;
   // NSLog(@&#34;i=%d&#34;,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(@&#34;pagewidth=%f&#34;,pageWidth);
    int page = floor((scr_anger.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
   // NSLog(@&#34;page=%d&#34;,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(@&#34;page=%d&#34;,page);
    }
}


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

#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender
{
    /*
   *Change the scroll view
   */
    //NSLog(@&#34;i=%d&#34;,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;

    ;
    /*
   *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;

      ;

      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&#39;t have a superview.
    ;

    // Release any cached data, images, etc that aren&#39;t in use.
}

- (void)viewDidUnload {
    ;
    ;

    ;
    ;
      // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    ;
}

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p> <a href="http://www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-14154" rel="noreferrer noopener nofollow">This</a>是一个关于 UIScrollView 和 IPageControl 组合的非常好的教程。但我自己寻找更多动态设计的教程(如 ios 应用程序的主页 - 我自动递增 - 递减)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何为uiscrollview添加页面控件?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10089593/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10089593/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何为uiscrollview添加页面控件?