Implementation code of viewing large picture in IOS development

Time:2021-10-18

Implementation code of viewing large picture in IOS development

This project is taken from the teaching project of wisdom podcast, and added the author’s modification and polishing.

1. Project name: view large drawing

2. Project screenshot display

3. Project function

  • Slide left and right to view the picture
  • Support zoom function
  • Click the middle button to move the picture

4. Project code

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  //Set content size
  self.scrollView.contentSize = self.imageView.frame.size;

  //Set
  self.scrollView.delegate = self;

  //Set最大和最小的缩放比例
  self.scrollView.maximumZoomScale = 2.0;
  self.scrollView.minimumZoomScale = 0.2;

  //Set边距
  self.scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);

  //Do not display horizontal scroll marks
  self.scrollView.showsHorizontalScrollIndicator = NO;

  //Do not display vertical scroll marks
  self.scrollView.showsVerticalScrollIndicator = NO;

  //Offset position
  self.scrollView.contentOffset = CGPointMake(0, -100);

  //Cancel spring effect
  self.scrollView.bounces = NO;

  //Set button
  UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
  btn.center = self.view.center;

  [self.view addSubview:btn];

  //Set button的监听方法
  [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

}

 //Move offset position of large drawing
- (void)click
{
  //Fetch offset
  CGPoint offset = self.scrollView.contentOffset;

  offset.x += 20;
  offset.y += 20;

  //Update contentoffset
  self.scrollView.contentOffset = offset;
}


#Pragma mark - proxy method of uiscrollview

//1 > Scrollview needs to know who to zoom

/**
 *Called when the user starts dragging the Scrollview
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
  Nslog (@ "start dragging");
}

/**
 *Called whenever the Scrollview is scrolling
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  Nslog (@ "scrolling% @", nsstringfromcgpoint (Scrollview. Contentoffset));
}

/**
 *Called when the user uses the kneading gesture
 *
 *@ return the returned control is the one that needs to be scaled
 */
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
  Nslog (@ "start scaling");
  return self.imageView;
}

/**
 *Called when zooming
 */
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
  Nslog (@ "Scaling");
}
@end

5. Code snippets that must be mastered in this project

Set outer margin


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

Create button with type


 UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];

Move offset of scroll content


- (void)click
{
  CGPoint offset = self.scrollView.contentOffset;

  offset.x += 20;
  offset.y += 20;

  self.scrollView.contentOffset = offset;
}

6. Notes

Reason why Scrollview cannot scroll:

  1. Contentsize is not set
  2. scrollEnabled = NO
  3. No touch event received: userinteractionenabled = no
  4. The AutoLayout function is not cancelled (if a child control of Scrollview is added to the storyboard, AutoLayout must be cancelled in order to scroll through the Scrollview)

Properties of Scrollview

@property(nonatomic) UIEdgeInsets contentInset;  This property can add an additional scroll area in the four weeks of uiscrollview
@property(nonatomic) CGPoint contentOffset; 
This property is used to indicate the position of the uiscrollview scroll

@property(nonatomic) CGSize contentSize; 
This attribute is used to indicate the size and scrolling range of the uiscrollview content (how far it can be scrolled)

@property(nonatomic) BOOL bounces;
Sets whether the uiscrollview requires a spring effect

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled; 
Sets whether the uiscrollview can scroll

@property(nonatomic) BOOL showsHorizontalScrollIndicator;
Show horizontal scroll bar

@property(nonatomic) BOOL showsVerticalScrollIndicator;
Show vertical scroll bar

When do I need a proxy for Scrollview?

When we want to do some specific operations when uiscrollview is scrolling or scrolling to a certain position or stopping scrolling, we need to be able to monitor the whole scrolling process of uiscrollview.

In other words, to monitor the scrolling process of uiscrollview, you must first set a proxy object (controller) for uiscrollview, and then know the scrolling process of uiscrollview through the proxy.

Uiscrollview defines the methods to be implemented by delegate (the methods to listen to the Scrollview) in the uiscrollviewdelegate protocol. Therefore, if you want to become the delegate of uiscrollview, you must abide by the uiscrollviewdelegate protocol, and then implement the corresponding methods in the protocol to listen to the rolling process of uiscrollview.

Generally, the controller of uiscrollview is set to delegate of uiscrollview.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

Recommended Today

Swift bottom layer exploration (III): pointer

IOS memory partition image.png Stack The stack area is to store the current local variables and the context during the operation of the function. func test() { var age: Int = 10 print(age) } test() (lldb) po withUnsafePointer(to: &age){print($0)} 0x00007ffeefbff3d8 0 elements (lldb) cat address 0x00007ffeefbff3d8 &0x00007ffeefbff3d8, stack address (SP: 0x7ffeefbff3b0 FP: 0x7ffeefbff3e0) SwiftPointer.test() -> […]