Realization of IOS Point-praise function based on Uicontrol control _ios

Source: Internet
Author: User
Tags uicontrol uikit

In development, there may be a lot of times need to do a bit of praise needs, if the button implementation, the button as a system composite control, external is a view--"Uicontrol container, the interior contains Uilabel and UIImage, as well as some typesetting rules." It is difficult to use UIButton to do some of the "Praise" and "cancel the praise" when the effect of the switch.
But we also need UIButton-like event response mechanism.

What to do?

Right! is to use Uicontrol.

Uicontrol has two outstanding advantages here:

1. As the parent control of UIButton, it has the same event response mechanism as UIButton

2. As a simple child control for UIView, with potential as a container view

Design idea: Implement a custom control, inherit Uicontrol, which contains some views, what the view is determined by your needs, my two pictures. Then, when the event responds, the two pictures are switched. Toggle animations can be customized. This will achieve a switch effect of a great degree of freedom of the point of Praise button Oh!

The reference code is as follows:

#import <UIKit/UIKit.h> 
typedef ns_enum (Nsinteger, Uicontrolflagmode) { 
  Flagmodelno, 
  Flagmodelyes, 
  Flagmodeldefalt 
}; 
@interface Uicontrolflagview:uicontrol 
@property (nonatomic, Strong) uiimage*nostateimg; 
@property (nonatomic, Strong) uiimage*yesstateimg; 
@property (nonatomic, Strong) uiimage*defaultstateimg; 
@property (nonatomic, assign) Uicontrolflagmode flag; 
-(void) Setflag: (uicontrolflagmode) flag withanimation: (BOOL) animation; 
@end 

M File:

#import "UIControlFlagView.h" @interface Uicontrolflagview () @property (nonatomic, strong) UIIMAGEVIEW*NOSTATEIMGV; 
@property (nonatomic, strong) UIIMAGEVIEW*YESSTATEIMGV; 
@property (nonatomic, strong) UIIMAGEVIEW*DEFAULTSTATEIMGV; 
  @end @implementation Uicontrolflagview-(ID) initWithFrame: (CGRect) frame {self = [super Initwithframe:frame]; 
if (self) {//initialization code} return self; }-(void) Setnostateimg: (UIImage *) nostateimg {if (!SELF.NOSTATEIMGV) {SELF.NOSTATEIMGV = [[Uiimageview All 
    OC] InitWithFrame:self.bounds]; 
    Self.noStateImgV.contentMode = Uiviewcontentmodecenter; 
    [Self AddSubview:self.noStateImgV]; 
  Self.flag = Flagmodelno;//default style} self.noStateImgV.image = nostateimg; 
_nostateimg = nostateimg;  }-(void) Setyesstateimg: (UIImage *) yesstateimg {if (!SELF.YESSTATEIMGV) {SELF.YESSTATEIMGV = [[Uiimageview 
    Alloc] initWithFrame:self.bounds]; Self.yesStateImgV.contentMode = UiviewconTentmodecenter; 
    [Self AddSubview:self.yesStateImgV]; 
  Self.yesStateImgV.alpha = 0.0; 
  } self.yesStateImgV.image = yesstateimg; 
_yesstateimg = yesstateimg;  }-(void) Setdefaultstateimg: (UIImage *) defaultstateimg {if (!SELF.DEFAULTSTATEIMGV) {SELF.DEFAULTSTATEIMGV 
    = [[[Uiimageview alloc] initWithFrame:self.bounds]; 
    Self.defaultStateImgV.contentMode = Uiviewcontentmodecenter; 
  [Self AddSubview:self.defaultStateImgV]; 
  } self.defaultStateImgV.image = defaultstateimg; 
_defaultstateimg = defaultstateimg; 
    }-(void) Setflag: (uicontrolflagmode) flag withanimation: (BOOL) Animation {if (animation) {//no-->yes if (_flag = = Flagmodelno && flag = = Flagmodelyes) {self.yesStateImgV.transform = Cgaffinetransformmak 
     Escale (0.1f, 0.1f); 
       [UIView animatewithduration:0.3 animations:^{self.noStateImgV.alpha = 0.0; 
       Self.yesStateImgV.alpha = 1.0; Self.yesStateImgV.transform = CgaffinetransfOrmmakescale (1.0f, 1.0f); 
     Self.noStateImgV.transform = Cgaffinetransformmakescale (2.0f, 2.0f); 
       } completion:^ (BOOL finished) {self.yesStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f); 
     Self.noStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f); 
    }]; //yes-->no else if (_flag = = Flagmodelyes && flag = = Flagmodelno) {Self.noStateImgV.tra 
      Nsform = Cgaffinetransformmakescale (0.1f, 0.1f); 
        [UIView animatewithduration:0.3 animations:^{self.noStateImgV.alpha = 1.0; 
        Self.yesStateImgV.alpha = 0.0; 
        Self.yesStateImgV.transform = Cgaffinetransformmakescale (2.0f, 2.0f); 
      Self.noStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f); } completion:^ (BOOL finished) {self.yesStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1 
         .0f); 
    Self.noStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f);   }]; } else {//no-->yes if (_flag = = Flagmodelno && flag = = Flagmodelyes) {Self 
        . Nostateimgv.alpha = 0.0; 
        Self.yesStateImgV.alpha = 1.0; 
        Self.yesStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f); 
    Self.noStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f); //yes-->no else if (_flag = = Flagmodelyes && flag = = Flagmodelno) {Self.noStateImgV.alp 
      Ha = 1.0; 
      Self.yesStateImgV.alpha = 0.0; 
      Self.yesStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f); 
    Self.noStateImgV.transform = Cgaffinetransformmakescale (1.0f, 1.0f); 
} _flag = Flag; } @end

This is a simple implementation, the biggest advantage, but also the purpose of this article, is in the switch effect on the customization and freedom!
To make a point, I hope that we will be able to compound their hearts think of the dot-praise button!

I hope you are interested in this article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.