UIScrollView의 축소 원리 해석

6126 단어 【개발 관련】

코드로 축소 가능한 그림 만들기


//  ViewController.m

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property(weak,nonatomic)UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    //   scrollview      375*667(4.7   )
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    scrollView.minimumZoomScale = 0.02;
    scrollView.maximumZoomScale = 2;
    //     
    scrollView.delegate = self;

    //   imageview  ,                  
    UIImageView *imageView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ab179aaejw1f44f5h8np9j23402c0kjl"]];
    [scrollView addSubview:imageView];
    //      
    self.imageView = imageView;
    //              
    scrollView.contentSize = imageView.image.size;
    //            ,      
    scrollView.contentOffset = CGPointMake(0, -64);
    //   top  64
    scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);


}
//         
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}
@end
    :To support zooming, you must set a delegate for your scroll view. The delegate object must conform to the UIScrollViewDelegate protocol. In many cases, the delegate will be the scroll view’s controller class. That delegate class must implement the viewForZoomingInScrollView: method and return the view to zoom. The implementation of the delegate method shown below returns the value of the controller’s imageView property, which is an instance of UIImageView. This specifies that the imageView property will be zoomed in response to zoom gestures, as well as any programmatic zooming.

축소를 지원하기 위해서 UIScroll View Delegate 프로토콜을 준수해야 한다는 뜻입니다.많은 경우, 의뢰는 스크롤 보기 컨트롤러 종류입니다.이 의뢰 클래스는viewForZoomingInScrollView: 방법을 실현하고 축소 보기로 돌아가야 합니다.다음 의뢰 방법의 실현은 이미지 뷰 속성을 되돌려줍니다. 이것은 UIImageView입니다.여기서 imageView 속성이 배율의 영향을 받도록 지정합니다.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{
    return self.imageView;
}

데이터는 여기를 클릭하여 이전 글의 그림을 참고하십시오
확대/축소
  • scrollView의delegate를 설정하고 간단한 코드를 실현하면scrollView는 축소할 수 있다
  • 이 코드들의 본질은scrollView가 누구를 축소하는지 알려주는 것이다
  • 보기 차원 구조를 이용하여 축소된 scrollView와 이미지 뷰의position(center)과bounds의 변화를 관찰
  • 축소가 끝난 후scrollView 자체의 프레임은 변하지 않았다
  • 축소가 끝난 후에도 이미지뷰 자체의 bounds는 변하지 않았다
  • 축소가 끝난 후 이미지뷰의center에 변화가 생겼습니다
  • 누구를 축소했는지 알게 되면 스크롤View는 내부에서 축소 목표를 수정하는transfrom을 통해 축소 효과를 실현한다
  • so.확대/축소가 발생하면 하위 컨트롤의 센터의 Position의 x와 y만 변경되며, x와 y의 크기를 변경하여 확대/축소되며, x, y가 커지면 확대됨을 나타냅니다.

    좋은 웹페이지 즐겨찾기