Sagit.Framework For IOS automatic layout tutorial: 8. Uiview common events: click, double-click, long press, drag, slide, screen edge sideslip, timing events.

Time:2020-10-25

preface:

This section introduces the general events of uiview.

Common events are available on all UI.

1. Uiview event: Click

definition:

//Click events that can be attached (when archived in keyValue, parameters cannot be transferred (memory address is invalid), only runtime attributes can be saved)
typedef  void(^OnViewClick)(id view);
#Pragma mark extended system events - Click
//! click event interval (in seconds)
-(NSInteger)clickInterval;
//! set the interval between click events (in seconds)
-(UIView*)clickInterval:(NSInteger)sencond;
//! execute click events
-(UIView*)click;
//! bind event: specify the event name, controller name, or other UI events, such as: Age.click  (age is the name of other UI)
-(UIView*)addClick:(NSString*)event;
//! bind event and specify target
-(UIView*)addClick:(NSString *)event target:(UIViewController*)target;
//! bind events in the form of code blocks
-(UIView*)onClick:(OnViewClick)block;
//! remove binding click events
-(UIView*)removeClick;

Example usage 1: direct events

//Add UI and add click events, and then trigger the click of another UI in the event
            [[[[cView addUIView:@"level4"] width:154 height:154] relate:LeftTop v:435 v2:538] onClick:^(UIView* view) {
                [winView click];
            }];

Example usage 2: trigger method

[[STLastImageView image:@"btn_sign"] addClick:@"sign:"];
//Sign in
-(void)sign:(UIButton*)btn
{
。。。。。。。
}

Example usage 3: jump controller.

Example usage 4: set allowed click interval (2 seconds)

[[[STLastButton titleColor:ColorWhite] backgroundImage:@"btn_full_middle"] clickInterval:2];

2. Uiview event: double click

definition:

#Pragma mark extended system events - double click
//! execute double click event
-(UIView*)dbClick;
//! bind event: specify the event name, controller name, or other UI events, such as: Age.click  (age is the name of other UI)
-(UIView*)addDbClick:(NSString*)event;
//! bind event and specify target
-(UIView*)addDbClick:(NSString *)event target:(UIViewController*)target;
//! bind events in the form of code blocks
-(UIView*)onDbClick:(OnViewClick)block;
//! remove binding double click event
-(UIView*)removeDbClick;

Usage is consistent with click.

3. Uiview event: long press

definition:

typedef  void(^OnLongPress)(id view);
#Pragma mark extended system event - long press
//! long press event
-(UIView*)longPress;
//! bind event: specify the event name, controller name, or other UI events, such as: Age.click  (age is the name of other UI)
-(UIView*)addLongPress:(NSString*)event;
//! bind event and specify target
-(UIView*)addLongPress:(NSString *)event target:(UIViewController*)target;
//! bind events in the form of code blocks
-(UIView*)onLongPress:(OnLongPress)block;
//! remove binding long press event
-(UIView*)removeLongPress;

Example usage:

 

Usage and click are basically the same.

4. Uiview event: drag

Definition: [PS: added allowed drag direction]

typedef  Bool(^OnViewDrag)(id view,UIPanGestureRecognizer *recognizer);
#Pragma mark extended system event drag
//! execute drag event
-(UIView*)drag;
//! bind event: specify the event name, controller name, or other UI events, such as: Age.drag  (age is the name of other UI)
-(UIView*)addDrag:(NSString*)event;
//! bind event and specify target
-(UIView*)addDrag:(NSString *)event target:(UIViewController*)target;
//! bind events in the form of code blocks
-(UIView*)onDrag:(OnViewDrag)block;-(UIView *)onDrag:(OnViewDrag)block direction:(DragDirection)direction
//! remove bound drag event
-(UIView*)removeDrag;

Example usage:

[sagit onDrag:^BOOL(UIView* view, UIPanGestureRecognizer *recognizer) {
        if(recognizer.state==UIGestureRecognizerStateEnded)
        {
            if(view.frame.origin.x>view.OriginFrame.origin.x)
            {
                //Left
                NSLog(@"to left");
            }
            else
            {
                //Right
                NSLog(@"to right");
            }
        }
        return true;
        } direction:DragToLeftRight];

explain:

1. The coordinate, start state and end state of the slide can be obtained through the recognizer.

2. Through the original coordinates and existing coordinates of the view, you can determine the direction of dragging.

3. By default, the dragged UI elements will move with the gesture (return true), but return false will not move with the gesture. 4. When you specify a direction, the default drag ends and the position is restored to the original position.

Other usage is the same as above.

5. Uiview events: sliding

definition:

Typedef void (^ onviewslide) (ID view, uishipgesturerecognizer * recognizer); ᦇ pragma mark extends system event sliding

//! bind event: specify the event name, controller name, or other UI events, such as: Age.drag  (age is the name of other UI)
-(UIView*)addSlide:(NSString*)event;
//! bind event and specify target
-(UIView*)addSlide:(NSString *)event target:(UIViewController*)target;
//! bind events in the form of code blocks
-(UIView*)onSlide:(OnViewSlide)block;
//! remove binding event
-(UIView*)removeSlide;

Example usage:

    [sagit onSlide:^(id view, UISwipeGestureRecognizer *recognizer) {
        [Sagit.MsgBox prompt:STNumString(recognizer.direction)];
    }];

You can get the direction of the gesture sliding.

6. Screen edge sideslip: left slide | right slide

[added on September 12, 2020]

definition:

#Pragma mark extended system event - screen sideslip (left edge slide)
//! bind event: specify the event name, controller name, or other UI events, such as: Age.drag  (age is the name of other UI)
-(UIView*)addScreenLeftEdgeSlide:(NSString*)event;
//! bind event and specify target
-(UIView*)addScreenLeftEdgeSlide:(NSString *)event target:(UIViewController*)target;
//! bind events in the form of code blocks
-(UIView*)onScreenLeftEdgeSlide:(OnScreenEdgeSlide)block;
//! remove binding event
-(UIView*)removeScreenLeftEdgeSlide;

#Pragma mark extended system event - screen sideslip (right edge slide)
//! bind event: specify the event name, controller name, or other UI events, such as: Age.drag  (age is the name of other UI)
-(UIView*)addScreenRightEdgeSlide:(NSString*)event;
//! bind event and specify target
-(UIView*)addScreenRightEdgeSlide:(NSString *)event target:(UIViewController*)target;
//! bind events in the form of code blocks
-(UIView*)onScreenRightEdgeSlide:(OnScreenEdgeSlide)block;
//! remove binding event
-(UIView*)removeScreenRightEdgeSlide;

Examples of use:

[[[[sagit addUIView:nil]  width:1 height:400] toCenter] block:^(UIView* view) {
        [view onScreenLeftEdgeSlide:^(UIView* view, UIScreenEdgePanGestureRecognizer *recognizer) {
             if(recognizer.state==UIGestureRecognizerStateEnded || recognizer.state==UIGestureRecognizerStateCancelled)
                {
                    [view backtoirigin]; // return to the original position.
                }
        }];
        [view onScreenRightEdgeSlide:^(id view, UIScreenEdgePanGestureRecognizer *recognizer) {
            
        }];
     
    }];

Add events.

Note: for the screen edge sliding event, the coordinates of uiview should be close to the screen edge, otherwise, the event cannot be triggered.

There are only left and right side slip events. Up and down are mobile phone system events and cannot be occupied.

7. Timed events

When a UI needs to update the display content regularly, the timing event can make it easy for you to complete the function code:

Event definition:

//! timer event.
typedef  void(^OnTimer)(id view,NSTimer *timer, NSInteger count);
#Pragma mark timer event
//! bind events in the form of code blocks
-(UIView*)onTimer:(OnTimer)block;
-(UIView*)onTimer:(OnTimer)block interval:(double)sencond;
//! remove binding event
-(UIView*)removeTimer;

Examples of use:

[[[status addLabel:nil text:[NSDate.beiJinDate toString:@"HH:mm"] font:15*STStandardScale color:color] toCenter] onTimer:^(UILabel* view,NSTimer *timer, NSInteger count) {
                                    //Time
                                    [view text:[NSDate.beiJinDate toString:@"HH:mm"]];
                                    //Battery
                                    CGFloat batteryLevel = [UIDevice currentDevice].batteryLevel;
                                    [battery text:STString(@"%.f%%",batteryLevel*100)];
                                    //wifi
                                    Reachability *reach=[Reachability reachabilityWithHostName:@"www.baidu.com"];
                                    NetworkStatus status= reach.currentReachabilityStatus;
                                    if(status==ReachableViaWiFi)
                                    {
                                        [wifi text:@"Wi-Fi"];
                                    }
                                    else
                                    {
                                        [wifi text:@"No Wifi"];
                                    }
                                } interval:60];

 

Other events:

Other events are UI specific, such as:

Long press copy of uilabel to save the event.

Long press save of uiimage to save the event.

Onswitch switching event of uiswitch.

Wait… Special points will be introduced separately.

 

Recommended Today

Layout of angular material (2): layout container

Layout container Layout and container Using thelayoutDirective to specify the layout direction for its child elements: arrange horizontally(layout=”row”)Or vertically(layout=”column”)。 Note that if thelayoutInstruction has no value, thenrowIs the default layout direction. row: items arranged horizontally.max-height = 100%andmax-widthIs the width of the item in the container. column: items arranged vertically.max-width = 100%andmax-heightIs the height of the […]