国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ios之UIScrollView

2019-11-14 18:21:56
字體:
來源:轉載
供稿:網友

UIScrollView 

很多ios的應用軟件都會滾動顯示單個或多個圖片,UIScrollView控件主要就是用來

滿足這種需求的,展示一張超出屏幕的圖片

 

 UIScorllView控件有三個與顯示相關的重要屬性:contentSize、contentInset和contentOffSet。

     UIScrollView *scrollView=[[UIScrollView alloc] init];

  UIImageView *imageView=[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"1.jpg"]];

1. 屬性contentSize表示UIScrollView中內容視圖的大小,返回的是一個CGSize結構體

  類型,該結構體包含width和height兩個成員

  scrollView.contentSize = imageView.frame.size;

  此時運行程序,可見只能顯示圖片的一部分,然后選中圖片可以任意移動圖片,實現

  滾動屏幕的效果。

2. 屬性contentInset用于在UIScrollView控件中的內容視圖周圍添加邊距,返回的是一

  個UIEdgeInsets結構體類型,該結構體包含top、left、bottom和right四個成員,表示

  四個邊距,這里設置上邊距和左邊距30個像素,

  scrollView.contentInset = UIEdgeInsetsMake(30, 30, 0,0);

  運行程序,滾動圖片可見內容視圖離上邊和左邊都有一個空白的邊距

3 .屬性contentOffSet是內容視圖坐標原點和UIScrollView坐標原點的偏移量,可以改

  變顯示內容的顯示區域,該屬性返回一個CGPoint結構體類型

 

 

 其他屬性

 設置反彈

 scrollView.bounces = NO;

設置滾動條的樣式

  [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack];

設置是否顯示平行滾動條

  scrollView.showsHorizontalScrollIndicator = NO;

設置是否顯示垂直滾動條

  lscrollView.showsVerticalScrollIndicator = NO;

 

 

三張圖片的滑動

- (void)viewDidLoad

{

    [super viewDidLoad];

    UIScrollView *scrollView=[[UIScrollView alloc] init];

    CGRect frame=self.view.frame;

    //設置大小和位置   

     scrollView.frame=frame;

   //加載圖片

    UIImage *image1=[UIImage imageNamed:@"1.jpg"];

    UIImage *image2=[UIImage imageNamed:@"2.jpg"];

    UIImage *image3=[UIImage imageNamed:@"3.jpg"];

    self.array=@[image1,image2,image3];    CGSizesize=CGSizeMake(self.array.count*self.view.frame.size.width,self.view.frame.size.height);

    //顯示內容大小

    scrollView.contentSize=size;

 

    for(int i=0;i<self.array.count;i++)

       

    {

        UIImageView *imageView=[[UIImageView alloc] initWithImage:self.array[i]];

        imageView.contentMode=UIViewContentModeScaleaspectFit;

      //圖片顯示的區域       

       imageView.frame=CGRectMake(i*self.view.frame.size.width,0,self.view.frame.size.width, self.view.frame.size.height);

      [scrollView addSubview:imageView];

    }

    scrollView.pagingEnabled=YES;

    [self.view addSubview:scrollView];

}

 

 

 

實現兩指縮放功能

1.要遵守<UIScrollViewDelegate>協議

2.方法里面給self.delegate賦值    self.scrollView.delegate = self;

3.實現方法  //那張圖片需要縮放

     - (UIView *)viewForZoomingInScrollView:(UIScrollView*)scrollView

    {

     Return self.imageView;

  }

 

  在viewDidLoad里設置可以縮放的比例

   scrollView.minimumZoomScale = 0.1;

   scrollView.maximumZoomScale = 1.0;

 

完整代碼:

#import "TRViewController.h"

@interface TRViewController () <UIScrollViewDelegate>

@PRoperty (nonatomic, strong)UIImageView *imageView;

@property (nonatomic, strong)UIScrollView *scrollView;

@end

- (void)viewDidLoad

 {

   [super viewDidLoad];

  UIScrollView *scrollView = [[UIScrollViewalloc]init];

   self.scrollView = scrollView;

   //添加顯示內容

   UIImageView *imageView=[[UIImageViewalloc]initWithImage:[UIImage imageNamed:@"qwer.jpg"]];

  self.imageView = imageView;

  [self.scrollView  addSubview:imageView];

  self.scrollView.frame = self.view.frame;

  //將self.scrollView添加到父視圖

  [self.viewaddSubview:self.scrollView];

  //設置scrollView顯示內容的大小

  self.scrollView.contentSize = imageView.frame.size;

   self.scrollView.contentInset = UIEdgeInsetsMake(20, 20,0,0);

  //scrollView的其他屬性

   //設置反彈

   self.scrollView.bounces = NO;

   //設置滾動條的樣式.

  [self.scrollView  setIndicatorStyle:UIScrollViewIndicatorStyleBlack];

  //設置是否顯示平行滾動條

  self.scrollView.showsHorizontalScrollIndicator = NO;

  //設置是否顯示垂直滾動條

  self.scrollView.showsVerticalScrollIndicator = NO;

  //給scrollView的委托賦值

  self.scrollView.delegate = self;

  //設置可以縮放的比例

  self.scrollView.minimumZoomScale =0.1

  self.scrollView.maximumZoomScale = 1.0;

}

 //實現方法 那張圖片需要縮放

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

   Return self.imageView;

}

@end


上一篇:iOS開發--圖片美化

下一篇:ios之UIImageView

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 常宁市| 佛冈县| 垫江县| 遂平县| 泊头市| 河池市| 靖安县| 龙泉市| 京山县| 宾阳县| 大新县| 江北区| 广元市| 南康市| 长宁区| 奎屯市| 梓潼县| 黄浦区| 莱芜市| 碌曲县| 嫩江县| 芦山县| 弥渡县| 新宾| 滦平县| 扬州市| 舟山市| 河西区| 和林格尔县| 隆回县| 辽宁省| 杂多县| 犍为县| 秦皇岛市| 工布江达县| 江门市| 旬阳县| 抚宁县| 花莲市| 六盘水市| 洛浦县|