向左和向右滑动以加载项目

时间:2015-06-19 02:31:10

标签: ios uigesturerecognizer

我有一个数组,其中包含10个项目,即1,2,3,4,5,6,7,8,9,10。当我加载页面时,项目位于3.所以当用户向左滑动时,它将转到4并向右滑动它将转到2.我可能知道该怎么做吗?

newsID=[[NSMutableArray alloc]init];
[newsID addObject:@"1"];
[newsID addObject:@"2"];
[newsID addObject:@"3"];
[newsID addObject:@"4"];
[newsID addObject:@"5"];
[newsID addObject:@"6"];
[newsID addObject:@"7"];
[newsID addObject:@"8"];
[newsID addObject:@"9"];
[newsID addObject:@"10"];
UISwipeGestureRecognizer * swipeleft=
    [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
UISwipeGestureRecognizer * swiperight=
    [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];

2 个答案:

答案 0 :(得分:3)

试试这个。 我正在使用一个UILabel并在滑动时更改它的文本。并使用一个int变量来跟踪滑动。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pl.com.mycompany.test</groupId>
    <artifactId>selenium-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>selenium-test</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>central</id>
            <url>http://maven-repo.myportal.pl:3080/artifactory/repo</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>snapshots</id>
            <url>http://maven-repo.myportal.pl:3080/artifactory/repo</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>internal</id>
            <name>Archiva Managed Internal Repository</name>
            <url>http://maven-repo.myportal.pl:3080/archiva/repository/internal</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <finalName>selenium-test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                    <!--<encoding>Cp1250</encoding> -->
                    <encoding>UTF-8</encoding>
                    <!--<encoding>ISO-8859-2</encoding> -->
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- Selenium -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.43.1</version>
        </dependency>
        <dependency>
            <groupId>pl.com.mycompany.selenium.util</groupId>
            <artifactId>selenium-listener</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.11</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.6.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>pl.com.mycompany.portal</groupId>
            <artifactId>commons-portal</artifactId>
            <version>RELEASE</version>
        </dependency>

        <dependency>
            <groupId>weblogic</groupId>
            <artifactId>weblogic</artifactId>
            <version>8.0</version>
        </dependency>

        <!-- TESTS -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert</artifactId>
            <version>1.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <!-- -->
    <profiles>
        <profile>
            <id>2db</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.11</version>
                        <configuration>
                            <properties>
                                <property>
                                    <name>listener</name>
                                    <value>pl.com.mycompany.selenium.util.TestNgRunSuiteListener</value>
                                </property>
                            </properties>
                            <systemProperties>
                                <property>
                                    <name>job_name</name>
                                    <value>${project.artifactId}</value>
                                </property>
                            </systemProperties>

                            <testFailureIgnore>true</testFailureIgnore>

                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

答案 1 :(得分:1)

以下是处理滑动手势的简单示例。在这里,我正在拿一个UILabel并在滑动时更改它的文本。

- (void)viewDidLoad 
  {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    _lblText.text=@"3";
    _lblText.textAlignment=NSTextAlignmentCenter;


    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];


    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

}

-(void)handleRightSwipe
{

    if ([_lblText.text intValue]>1)
    {
        _lblText.text=[NSString stringWithFormat:@"%d",[_lblText.text intValue]-1];
    }
    else
    {
        return;
    }
}

-(void)handleLeftSwipe
{

    if ([_lblText.text intValue]<10)
    {
        _lblText.text=[NSString stringWithFormat:@"%d",[_lblText.text intValue]+1];
    }
    else
    {
        return;
    }

}